15

Why is it that setting the prototype property of a constructor function to null does not prevent objects created from that function from calling through to methods on Object.prototype, in the same way that setting the prototype to Object.create(null) does?

That is, why is this the case:

function Foo(){}
Foo.prototype = null;
console.log(new Foo().toString); //outputs function toString() { [native code] } (or whatever)

function Foo(){}
Foo.prototype = Object.create(null);
console.log(new Foo().toString); //output undefined
0

1 Answer 1

35

In short

Yes, your observation is correct - a function constructed with the new operator will always have an object prototype in this case Object.prototype and this is indeed unlike a function created with Object.create.


On why

One can see this behavior completely specified in the ES5 language specification on which JavaScript is based on. Let's see this.

In new:

Quoting the specification of the [[Construct]] method of functions that indicates how object creation using the new operator is performed we can see that the following is specified:

If Type(proto) is not Object, set the [[Prototype]] internal property of obj to the standard built-in Object prototype object as described in 15.2.4.

In Object.create:

On the other hand, if we check out The spec for Object.create we can see that Object.create(o) specifies:

Set the [[Prototype]] internal property of obj to O.

Which means we can set it, it also explicitly checks that it is null or Object in that algorithm (please do follow the link to the spec and read it :))

So the prototype of the objects called with new Foo is Object.prototype and not null. It is impossible to create objects with no prototype without Object.create using standard methods only.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for this answer, it clearly explains the situation. I'll read the spec more often :P
@Benjamin Gruenbaum guru!
@Bejamin Gruenbaum You wrote: "It is impossible to create objects with no prototype without Object.create using standard methods only." but you can do: function Foo() {} and then Foo.prototype = { proto: null }, and then new Foo() will be an object with no prototype, the same as Object.create(null). What did I miss here ?
@EvaCohen You can also do { __proto__: null } though that's not a mandatory part of JavaScript yet (they were talking about moving __proto__ from the annex to the spec and certain environment like Deno don't implement it)
@EvaCohen In any case - this answer predates __proto__ being a part of JavaScript (even in the annex) by several years.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.