11

could anyone please tell me, where in Javascript the difference between

MyClass.prototype = new Object(); //or ... = {}

and

MyClass.prototype = Object;

is? And if there is no difference in the result, which one is the best-practise-way-to-go?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
moxn
  • 1,790
  • 1
  • 15
  • 34
  • possible duplicate of [What is the reason to use the 'new' keyword here?](http://stackoverflow.com/questions/12592913/what-is-the-reason-to-use-the-new-keyword-here) – Bergi Feb 18 '14 at 21:09
  • 1
    @Bergi this question is more than 4 years old. The one you linked to is 2 years old... – moxn Feb 19 '14 at 22:10
  • The 1.5-year old answer is still valid, and this question is still found via search and links. It should point to other helpful answers… – Bergi Feb 20 '14 at 10:36
  • possible duplicate of [new MyObject(); vs new MyObject;](http://stackoverflow.com/questions/3034941/new-myobject-vs-new-myobject) – Ciro Santilli OurBigBook.com Sep 05 '15 at 10:11

3 Answers3

11

Your first two examples are completely equivalent:

MyClass.prototype = new Object(); // empty object
MyClass.prototype = {}; // empty object

Your third example is not valid, since you are assigning to the MyClass.prototype a reference to the Object constructor, and it's a function, not a new object.

I personally prefer the second, the object literal or initialiser syntax:

MyClass.prototype = {prop1: 'value', prop2: 'value2'};
//...
MyClass.prototype.foo = 'bar';
MyClass.prototype.method1: function () {/**/};

Edit: In response to your comment, an empty object literal { } essentially equivalent to new Object() because of this:

The production ObjectLiteral : { } is evaluated as follows:

  1. Create a new object as if by the expression new Object().
  2. Return Result(1).

For more details check the 11.1.5 section (Object Initialiser) of the ECMAScript Language Spec (pdf).

Edit: The third example won't produce any errors, but is not good at all, for example you can easily clobber the Object constructor function if you extend afterward the MyClass.prototype:

MyClass.prototype = Object;
MyClass.prototype.foo = 'bar';

Object.foo === MyClass.prototype.foo; // true
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • Hmhm, so if both examples don't produce errors of any kind and essentially do the same (as it actually is), it is just the laxity of the browser's javascript engine that allows the second example to work? – moxn Sep 14 '09 at 06:45
  • No, I know that {} == new Object(). What I meant by "second example" was ...prototype = Object; – moxn Sep 14 '09 at 06:59
2

It depends on Object. If it is a function you want to use the new Object() method. If it is a "virtual class" (defined using Object = {someProperty: someValue}) then you use the second method.

Some more pointers in this page on prototype inheritance in JavaScript

gnarf
  • 105,192
  • 25
  • 127
  • 161
0

MyClass.prototype.method1: function () {/**/};

Correction to the above: it should be

MyClass.prototype.method1 = function () {/**/}; 

(Note the equals sign after 'method1').

The colon is used only when the method definition is itself within an object definition, like:

var myObject = {myVar1: 10, myMethod1: function() { /* */};
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
JamieJag
  • 1,541
  • 2
  • 11
  • 18