0

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 ?

JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132

1 Answers1

1

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

Sam
  • 2,950
  • 1
  • 18
  • 26
  • @JavaDeveloper class is not a javascript object, maybe it's from any framework – Sam Mar 04 '14 at 20:36
  • var Person = Class.create(); Person.prototype = { initialize: function(name) { this.name = name; }, say: function(message) { return this.name + ': ' + message; } }; – JavaDeveloper Mar 04 '14 at 20:38
  • @JavaDeveloper "In early versions of Prototype, the framework came with basic support for class creation", it's a framework, Class is not a built-in object of javascript – Sam Mar 04 '14 at 20:45
  • @JavaDeveloper i don't know prototype framework so i guess Class.create() is fine with it, i prefer code in pure javascript or with jquery personally; with jquery for example you can add functions directly to the jQuery object prototype – Sam Mar 04 '14 at 20:52