I'm diving more into Prototypal Inheritance with JavaScript. When Object.Create() is in use to create objects, can someone show what is going on under the hood? Does Object.Create() depend on new and constructor functions behind the scenes?
-
"Under the hood" is an entirely different programming language used to implement the JS runtime, so things like "new" and "constructor" have different meaning, if any. – Mar 05 '18 at 18:14
-
Have you tried looking at the [v8 source?](https://github.com/v8/v8/blob/2310ea72b9de2a701f0b975a41d5710469472ae8/src/runtime/runtime-object.cc#L347) – mustachioed Mar 05 '18 at 18:15
-
2Here's that part of the spec to which the implementations must conform (semantically): https://tc39.github.io/ecma262/#sec-object.create This only describes the semantic requirements. Actual implementations can be, and likely are, very different. – Mar 05 '18 at 18:16
-
1The **[polyfill example on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#Polyfill)** should pretty well demonstrate the sort of things that are going on here. It does not handle the additional properties, just the basic creation, but it should be fairly clear. – Scott Sauyet Mar 05 '18 at 19:17
2 Answers
When
Object.create()
is in use to create objects, can someone show what is going on under the hood?
Low level details. Object.create
is pretty much a primitive operation - similar to what happens when an {}
object literal is evaluated. Just try to understand what it is doing.
That said, with new ES6 operations it could be implemented in terms of
function create(proto, descriptors) {
return Object.defineProperties(Object.setPrototypeOf({}, proto), descriptors);
}
Does
Object.create()
depend onnew
and constructor functions behind the scenes?
No, not at all. It's the reverse rather. The new
operator could be implemented as
function new(constructor, arguments) {
var instance = Object.create(constructor.prototype);
constructor.apply(instance, arguments);
return instance;
}

- 630,263
- 148
- 957
- 1,375
Object.create does not call "new" or the constructor function. It just set the prototype of the new object to the prototype of the object passed as parameter.
So
AnotherObject.prototype = Object.create ( Base.prototype )
creates the new object and set AnotherObject.__proto__ to Base.prototype
When you call "new", besides calling the "create" (above) it also calls the Base class's constructor.
To extend, you can extend the new object's prototype as
AnotherObject.prototype.anotherMethod = function() {
// code for another method
};
If you need a new new constructor for the new object you can create it like:
function AnotherObject() {
Base.call(this);
}
AnotherObject.prototype.constructor = AnotherObject;
-
I think you mean "instead" instead of "besides". That line is really confusing. – Bergi Mar 05 '18 at 18:39
-
No it is "besides". When you call new - it does a "create" + it calls the base class's constructor. It does both. Create does not call base class's constructor, "new" does. – Ari Singh Mar 05 '18 at 18:41
-
Oh that's what you meant. I fixed the comma to enable that interpretation of the wording :-) – Bergi Mar 05 '18 at 18:44