0

I have been working on web applications for very long time. Worked with most experienced technical architects. Everywhere I used javascript in object notation with namespaces.

var web = {};
web.app = {};
web.app.customer = {
    name: 'John',
    getName: function(){
        return 'The name is ' + this.name;
    }
};

document.write(web.app.customer.getName());

But, when I look for any object oriented javascript, we come with function and creating object of it by calling its constructor.

function Customer(name){
    this.name = name;
    this.getName = function(){
        return 'The name is ' + this.name;
    }
}

var cust = new Customer('Bob');
document.write(cust.getName());

I am not sure why architects advised me the former approach(namespace) always. I never used the latter approach, whereas the latter one is object oriented javascript.

  1. Cant we have object oriented javascript with namespace notation(former one)? If so can you create an object of that and inherit them.
  2. Why can't I have private fields in first approach(namespace)? If I declare any field with var, javascript error is thrown.
2
  • 1
    In the former, if you have 100 customers, do you write the getName function for each of them? Commented Mar 4, 2016 at 10:51
  • @RemcoGerlich - That's the question. Why do you want to have 100 customers? I never used this type, creating 100 customer objects in javascript. Could you please give me an example why do we go for creating number of objects in javascript? Commented Mar 6, 2016 at 6:58

1 Answer 1

0

The only difference between the two approaches is that the former (literal notation) creates one specific object, while the latter defines a constructor function which can be used to create objects. It's the difference between being able to only have one object (asterisk argument about object cloning here), and being able to instantiate many similar objects from the same template ("class").

Both are equally valid, depending on what you want. Both are equally object oriented.

Cant we have object oriented javascript with namespace notation(former one)? If so can you create an object of that and inherit them.

web.app.Customer = function (name) {
   ..
};

Why can't I have private fields in first approach(namespace)? If I declare any field with var, javascript error is thrown.

Because "private fields" don't exist in Javascript. You can use the characteristics of closures/variable scoping to emulate something like "private properties". But for that you obviously need a function. With an object literal notation, you don't get that.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. I have been working on web development javascript in asp.net and mvc for almost 9 years. But, I never used the latter approach, function and creating object of the function. I always used the literal notation. This is also advised. May I know why it is? Also, what did you use in your programming experience?
Have you ever used any Javascript library which requires you to use new at any point? Again: it's the difference between a singleton object and a constructor function which can construct many new objects. If you've never found a need for the latter, you probably haven't written very complex reusable code, or you weren't aware that that might have benefitted you.
Thanks. I appreciate your quick response. Can you share me your experience with an example for latter one. Based on requirement is more generic. I am trying to learn JavaScript against the realtime requirements.
Have you ever written any code in any language that uses classes and constructors? The reasons for using them are the same anywhere and are fundamental parts of OOP.
Yes Indeed. I created objects in C#. But never in JavaScript.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.