Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: what are some guidelines on how to define new data types?

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.

like image 748
pgpb.padilla Avatar asked Apr 07 '13 00:04

pgpb.padilla


1 Answers

I'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 comes down to taste. Be consistent and don't complicate things.

like image 160
Adam Bergmark Avatar answered Oct 30 '22 11:10

Adam Bergmark