2

I understand that there are multiple ways to create an object in javascript and I have been reading that object literal syntax is generally preferred. (Correct?)

What I haven't been able to figure out is if there is ever a reason to use any of the other ways to create objects, such as a custom constructor function (var p = new Person("Adam"))? Is it true to use a custom constructor function only if I want private variables or to add methods or properties to its prototype? Is there no way to do these in a literal?

Colin Brock
  • 21,267
  • 9
  • 46
  • 61
user895400
  • 368
  • 1
  • 7
  • 18
  • prototypal inheritance, class extension, simplified construction of repetitive objects, type checking via `instanceof`... to name a few. – zzzzBov Apr 03 '12 at 14:24

3 Answers3

2

The discussion usually is about to prefer

var myObject = {};

over

var myObject = new Object();

If you however create your own constructor functions you are perfectly allowed to instantiate them with the new keyword, nothing controversial there.

Willem D'Haeseleer
  • 19,661
  • 9
  • 66
  • 99
2

You can use the custom constructor function when you want to create instances of objects, similar to Java.

For example:

function MyObj(x){
   this.x = x;
}

MyObj.prototype.printX = function(){
   alert(this.x);
}

var obj1 = new MyObj("hello");
var obj2 = new MyObj("hello2");
obj1.printX();//prints hello
obj2.printX();//prints hello2

Now I have two instances of this object. If I used String literals I would need to clone the object into a new var in order to get another instance.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

The preferred method would be to use JSON: var p = { "name":"Adam" };

If you have a lot of member variables you need to initialize, or will be using a lot of objects (such as an array of them), etc. then it only makes sense to go ahead and create a function (constructor) which will do all of this for you. Unless you want your code to look like this:

var a = { "name":"Adam", "age":23, "city":"Boston" };
var b = { "name":"Jeff", "age":24, "city":"San mateo" };
var c = { "name":"Aaliyah", "age":25, "city":"New York" };
var d = { "name":"Mary", "age":26, "city":"Dallas" };
Authman Apatira
  • 3,994
  • 1
  • 26
  • 33
  • JSON seems like the wrong terminology. JSON doesn't allow methods for one. – user895400 Apr 03 '12 at 14:20
  • @user895400, JSON means JavaScript Object Notation. This is JavasScript, and we're using it's object notation. JSON is correct. – zzzzBov Apr 03 '12 at 14:21
  • Plus it does allow methods; you can create a function within a JSON object (though it's not good practice). Typically, JSON is meant to be neat and compact. – Authman Apatira Apr 03 '12 at 14:50
  • 1
    JSON is a data transfer format. It is a combination of array and object literal notation. If it means the same thing as object literals, why correct me and say that the preferred format is JSON, why not just agree with me that object literals are preferred? – user895400 Apr 03 '12 at 15:06
  • @zzzzBov: JSON is a subset of JavaScript's object literal syntax, so is not really correct here. – Tim Down Apr 03 '12 at 15:16
  • @TimDown, I suppose I was confused as to what was being discussed. The example AuthmanApatira provided is JSON (specifically `{"name":"Adam"}`). It seems user895400 was considering the general form of JavaScript objects (`{foo: bar, fizz: function () {...}}`) which is not JSON (although it *is* part of JavaScript's object notation). – zzzzBov Apr 03 '12 at 15:39