1

I have heard that the Object.create(); method creates a new object and inherits the PROTOTYPE of the Object that is passed as the first parameter.

However I've seen both of these scenarios:

var newObj = Object.create(BaseObject);

And

var newObj = Object.create(BaseObject.prototype);

The first scenario is inheriting the prototype of BaseObject and the second is doing the same, is there any difference? It seems like no, there is not.

Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
user3412869
  • 1,395
  • 2
  • 10
  • 11
  • Is the second inheriting `BaseObject`'s parent? I'm not sure. – DontVoteMeDown May 07 '14 at 14:11
  • 2
    Here's the docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create – gen_Eric May 07 '14 at 14:11
  • 1
    If your two examples should imply that `BaseObject` represents the same object, then that would be very unusual. Typically the only time you'd pass the `.prototype` of an object is if the object is a function, and it would be rare to pass a function itself. – cookie monster May 07 '14 at 14:39
  • Could you show us a link with the first scenario? I think that's wrong. You won't set a constructor function as the prototype of an object. The capital B of BaseObject and BaseObject.prototype would indicate that BaseObject is a constructor function. More on what prototype is and how it can be used with constructor functions here: http://stackoverflow.com/a/16063711/1641941 – HMR May 07 '14 at 15:23

2 Answers2

0

Whatever you pass as the first argument to Object.create will be assigned to the prototype of your new object. So it really depends what BaseObject is, if you have functions defined on BaseObject then the first example is correct, however it is also possible to assign functions to the prototype of BaseObject in which case you should pass the prototype to Object.create.

var BaseObject = {
   doSomething: function() { alert('hi'); }
}

var newObj = Object.create(BaseObject);
newObj.doSomething() // Alerts hi

function BaseObject() {
}

BaseObject.prototype.doSomething = function() { alert('hi'); };

function ChildObject() {
}

ChildObject.prototype = Object.create(BaseObject.prototype);

var newObj = new ChildObject();
newObj.doSomething() // Alerts hi

It depends whether what you are extending is using the prototype and whether you intend to use new when creating objects. As ever MDN has a good example of building an inheritance chain using the prototype and Object.create.

slashnick
  • 26,167
  • 10
  • 55
  • 67
  • Thank you so much, can you look at @bergi comment and see if his wrong? It looks as if you are saying different things about the result of object.create();...You said "Whatever you pass as the first argument to Object.create will be assigned to the prototype of your new object." Whereas his answer says if you pass an object to the first parameter without specifying the prototype, it will inherit the constructor. I'm confused. But I think you're right I didn't think the object.create() could be used to inherit the constructor – user3412869 May 07 '14 at 14:36
  • 1
    No `Object.create()` wouldn't be used to inherit the constructor. `Object.create` will always return a new object, and the prototype of that new object will be whatever you passed `Object.create`. I think both me and @bergi are saying the same thing. – slashnick May 07 '14 at 14:46
  • I think the confusion comes from things like "*will be assigned to the prototype of your new object*". Do you know the [difference between "prototype" and `.prototype`](http://stackoverflow.com/questions/9959727/proto-vs-prototype-in-javascript)? – Bergi May 07 '14 at 14:49
  • So there is no difference between – user3412869 May 07 '14 at 16:00
  • Var newObj = Object.create(baseobj.prototype); and var newObj.prototype = Object.create(baseobj); – user3412869 May 07 '14 at 16:01
0
var newObj = Object.create(BaseObject);

You use this when you inherit from a single object, which here is located in the variable BaseObject (uncommon casing). More likely, you have seen

var newObj = Object.create(baseObject);
var newObj = Object.create(instanceOfBase);
var newObj = Object.create(BaseObject.prototype);

This just does the same, but the object from which your newObj will inherit is located at the .prototype property of BaseObject here. That is the common case for constructor functions:

var newObj = Object.create(BaseConstructor.prototype);

There is no difference in the behaviour of Object.create, only you pass a different argument.


The first scenario is inheriting the prototype of BaseObject

No, it does not. It creates a newObj that does inherit from the BaseObject object itself. If BaseObject is a function, that's likely not to be what you wanted.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375