JavaDeveloper
JavaDeveloper

Reputation: 5660

Understanding javascript object creation and prototype

Consider the following:

Are the following code piece equivalient ?

var foo = Class.create();
foo.prototype = {
    initialize : function() {};
    sayHello : function() {};
}

and

    var foo = Class.create();
    foo.prototype = {
        initialize : function() {};
    } 
   foo.prototype.sayHello : function() {};

Secondly, which one to prefer other the other ? when and why ?

Upvotes: -1

Views: 22

Answers (1)

Sam
Sam

Reputation: 2970

They're both wrong, they should be

var foo = new Object();
foo.prototype = {
  initialize : function() {},
  sayHello : function() {}
}

and

var foo = new Object();
foo.prototype = {
    initialize : function() {}
} 
foo.prototype.sayHello = function() {};

and yes they're the same

I prefer the first way for initialize because is more clear

Upvotes: 1

Related Questions