Reputation: 105
A quick but hard-to-google question:
var child = Object.create(parent.prototype);
var child = Object(parent.prototype);
Are they identical?
edit:
My question was raised by this two examples of inheritPrototype functions used to implement the Parasitic Combination Inheritance Pattern.
function inheritPrototype(childObject, parentObject) {
var copyOfParent = Object.create(parentObject.prototype);
copyOfParent.constructor = childObject;
childObject.prototype = copyOfParent;
}
http://javascriptissexy.com/oop-in-javascript-what-you-need-to-know/
function inheritPrototype(subType, superType){
var prototype = object(superType.prototype);
prototype.constructor = subType;
subType.prototype = prototype;
}
"Parasitic Combination Inheritance" in Professional JavaScript for Web Developers
Upvotes: 3
Views: 407
Reputation: 8325
No they are not identical see below:
Object.create(prototype)
will create a new prototype which inherits from the prototype
passed as argument
Object(obj)
or new Object(obj)
will create a new instance of obj
calling the constructor as well.
Now since prototypes are javascript objects as well, the difference can be minimal. However Object.create
can handle constructors which may need arguments, while new Object()
will not
Further info in this SO related answer (e.g differential inheritance, custom properties and so on)
References:
UPDATE after OP edit
In my OOP javascript framework Classy.js there is this polyfill around Object.create
for older browsers which might shed light on your further question between differences:
Create = Obj.create || function( proto, properties ) {
var Type = function () {}, TypeObject;
Type[PROTO] = proto;
TypeObject = new Type( );
TypeObject[__PROTO__] = proto;
if ( 'object' === typeOf(properties) ) defineProperties(TypeObject, properties);
return TypeObject;
}
This polyfill can handle arbitrary constructors (with arguments) like Object.create
can, and also make sure that the __proto__
is assigned correctly. The pattern using Object(prototype)
in question is an attempt to (weakly-)reference the prototype
.
Upvotes: 3
Reputation: 39260
New Object sets the child to be the parent:
var parent = {name : 'proto'};
var child = new Object(parent);
console.log(child === parent);//=true
//if I set child.name it will change parent.name
Object create returns an object instance that uses the first argument as it's proto (first prototype used in the prototype chain).
child = Object.create(parent);
console.log(child === parent);//=false
//I can set child.name because it will be shadowed
// and will not change parent.name
console.log(child.hasOwnProperty('name'));//=false
More on prototype can be found here: https://stackoverflow.com/a/16063711/1641941
Upvotes: 1
Reputation: 23816
No they are not same:
Object(X) or new Object(X) are same as Object.create(X.prototype)
Object(X)
will create object and run Constructor
( ie. the newly created object inherits from the constructor's prototype). Object.create(X.prototype)
is creating object with additionally running Constructor
.
Object(X) or new Object(X) are not as Object.create()
Object.create()
is creating object without running Constructor
( ie. it will create an object that doesn't inherit from anything)
Upvotes: 3