Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null prototype, Object.prototype and Object.create

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
like image 290
Duncan Avatar asked Aug 12 '13 23:08

Duncan


People also ask

What is object create null?

prototype while Object. create(null) doesn't inherit from anything and thus has no properties at all. In other words: A javascript object inherits from Object by default, unless you explicitly create it with null as its prototype, like: Object. create(null) . {} would instead be equivalent to Object.

What is the difference between prototype and __ proto __ explain with examples?

The prototype property is set to function when it is declared. All the functions have a prototype property. proto property that is set to an object when it is created using a new keyword. All objects behavior newly created have proto properties.

What is __ proto __ and prototype?

__proto__ is the actual object that is used in the lookup chain to resolve methods, etc. prototype is the object that is used to build __proto__ when you create an object with new : ( new Foo ).

What is the difference between object assign and object create?

Object. assign() provides shallow copying (Only properties and methods) and it will override the method and property declared. while Object. create() provides Deep copying provides prototype chain.


1 Answers

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.

like image 72
Benjamin Gruenbaum Avatar answered Oct 11 '22 04:10

Benjamin Gruenbaum