Difference between Object.create (prototype) and object (prototype)
Quick but tricky question:
var child = Object.create(parent.prototype);
var child = Object(parent.prototype);
Are they identical?
<i> change
My question was raised by these two examples of inheritPrototype functions used to implement parasitic combinational inheritance.
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 Combinational Inheritance" in Professional JavaScript for Web Developers
source to share
No, they are not identical, see below:
-
Object.create(prototype)
will create a new prototype which inherits fromprototype
passed as argument -
Object(obj)
ornew Object(obj)
will create a new instance ofobj
, calling the constructor.
Now, since prototypes are javascript objects too, the difference can be minimal. However, Object.create
can handle constructors that may need arguments, rather new Object()
than
More info in this SO answer (e.g. differential inheritance, custom properties, etc.)
Links
UPDATE after OP edit
My OOP Classy.js javascript framework has this polyfill around Object.create
for older browsers which might shed some light on your further question between the 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 it's __proto__
assigned correctly. The pattern using Object(prototype)
is an attempt to (weakly) reference prototype
.
source to share
No, they are not like that:
Object (X) or new object (X) is similar to Object.create (X.prototype)
Object(X)
will create an object and run it Constructor
(i.e. the newly created object inherits from the constructor prototype). Object.create(X.prototype)
creates an object with an additional start Constructor
.
Object (X) or new object (X) does not match Object.create ()
Object.create()
creates an object without starting Constructor
(i.e. it will create an object that doesn't inherit anything)
source to share
The new object sets the parent element:
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 instance of an object that uses the first argument as its prototype (the 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 details about the prototype can be found here: fooobar.com/questions/2287 / ...
source to share