I recently stumbled upon the Object.create()
method in JavaScript, and am trying to deduce how it is different from creating a new instance of an object with
Here are the steps that happen internally for both calls:
(Hint: the only difference is in step 3)
new Test()
:
new Object()
objobj.__proto__
to Test.prototype
return Test.call(obj) || obj;
// normally obj is returned but constructors in JS can return a value
Object.create( Test.prototype )
new Object()
objobj.__proto__
to Test.prototype
return obj;
So basically Object.create
doesn't execute the constructor.