Suppose you are creating the data type and expose its behavior.
Can you give some examples of when would you use:
a function & new:
// define new data type var CustomDataType= function(){ this.a='whatever'; this.doX= function(){/*some code*/}; } // create a new instance of our custom data type var obj= new customDataType();
an object literal & Object.create:
// define new data type var customDataType = { a: 'whatever', doX: function(){/*some code*/} } // create a new instance of our custom data type var obj= Object.create(customDataType);
a function that builds your object:
function customDataTypeFactory(options){ return { a: 'whatever', doX: function(){/*some code*/} } }; // create a new instance of our custom data type var obj= customDataTypeFactory(options);
I feel this could be labeled duplicate for: new
vs Object.create
but my main interest is not in discussing which one is better but rather to know if there are specific use cases where one should be preferred over the others.
I have read many posts on related questions and the book from Crockford: Javascript: the good parts. So far I have concluded that it is a matter of preference, tough the advice from Crockford resonates a lot with me to me: "try to avoid the features that are dangerous and unnecessary"... I'm talking about new
.
Suppose you are creating the data type and expose its behavior.
Can you give some examples of when would you use:
a function & new:
// define new data type var CustomDataType= function(){ this.a='whatever'; this.doX= function(){/*some code*/}; } // create a new instance of our custom data type var obj= new customDataType();
an object literal & Object.create:
// define new data type var customDataType = { a: 'whatever', doX: function(){/*some code*/} } // create a new instance of our custom data type var obj= Object.create(customDataType);
a function that builds your object:
function customDataTypeFactory(options){ return { a: 'whatever', doX: function(){/*some code*/} } }; // create a new instance of our custom data type var obj= customDataTypeFactory(options);
I feel this could be labeled duplicate for: new
vs Object.create
but my main interest is not in discussing which one is better but rather to know if there are specific use cases where one should be preferred over the others.
I have read many posts on related questions and the book from Crockford: Javascript: the good parts. So far I have concluded that it is a matter of preference, tough the advice from Crockford resonates a lot with me to me: "try to avoid the features that are dangerous and unnecessary"... I'm talking about new
.
-
Your first and third versions put properties directly on the new object. In your second version, the object inherits the properties. I don't know that there's any concrete answer to your question. The first one uses a constructor if you invoke using
new
, andinstanceof
will work to identify that object. The third one doesn't require the use ofnew
, butinstanceof
won't be as useful. The second one again inherits the properties, so building several objects that way will all share the data, and all the objects will observe updates to the inherited object. – user1106925 Commented Apr 7, 2013 at 1:09 - ...and don't get too hung up on Crockford's danger warnings. I think he worries a little too much. I mean, take it all in, but take it with a grain of salt. – user1106925 Commented Apr 7, 2013 at 1:11
-
1
this.doX=
should bedoX :
in the last two snippets – Adam Bergmark Commented Apr 7, 2013 at 5:49
1 Answer
Reset to default 7I'll start with the way I usually define classes:
function CustomDataType(a)
{
this.a = a;
this.b = 2;
}
CustomDataType.prototype = {
doX : function () {}
};
var obj = new CustomDataType(1);
I assign variables in the constructor because F.prototype = { a : [1,2,3]; }
is problematic, the array will be shared between instances unless the property is reinitialized in the constructor (problematic for all non-primitive types). And it's also needed when the property somehow depends on the arguments to the constructor. I declare methods in the prototype so that they are shared between all instances, if you use inheritance it means you can have super calls, it will also lead to less memory usage since the method only has to be allocated once (though that probably isn't a big issue).
I have never used Object.create
, I don't see any reason to do to that unless you are into some insane dynamic hackery.
I use object literals whenever it feels unnecessary to create a separate class for the objects, for instance if it's arguments to a methods (so callers don't have to explicitly instantiate something), or if it's some internal values that shouldn't be accessed by anyone else.
Your factory is essentially the same as the object literal. I suppose it can be useful if you need to create some jsonesque structure and set some default values.
I don't see why new
would be dangerous or unnecessary, in what context did he say that?
But I think a lot of it es down to taste. Be consistent and don't plicate things.