What is in Object.__proto__?

江枫思渺然 提交于 2019-12-02 19:39:06

The top of the object graph is formed to maintain as much consistency with the expectations set elsewhere in the spec.

Necessarily, there comes a point where normal object linkages cannot be used because you "run out of objects".

A basic understanding of JavaScript leads us to expect the [[Prototype]] of Object to be the prototype property of the function used to create the Object function-object.

We expect Functions to be created using the Function function-object, so...

Object.__proto__ === Function.prototype

Because we are at the top of the object graph and want to maintain consistency of expected behavior we configure the [[Prototype]] of Function to be Function.prototype.

Function.__proto__ === Function.prototype

Thus ensuring Function instanceof Function === true.

We can show that Function.prototype is a special function-object because:

Function.prototype.prototype === undefined

...and every user-defined function (other than fat-arrows) has an object on its prototype property.

Because of all the above:

Object.__proto__ === Function.__proto__

This may might look odd, but as previously noted, at the top of the object graph we have a limited set of candidate objects to point to.

TC-39 now needed to identify what the [[Prototype]] of the [[Prototype]] of Object was. Per the above we know that the [[Prototype]] of Object is Function.prototype.

In some sense we are now above Function.prototype in the object graph, so a special Object instance (the "prototype object") was chosen to be this value.

This means that the top of every prototype chain can be conveniently tied up with Object.prototype.

This of course, also meets the desireable requirement that everything "is an object".

Object.__proto__.__proto__ === Object.prototype 

At this point we need to complete the object graph, so we set the [[Prototype]] of Object.prototype to be null.

Object.__proto__.__proto__.__proto__ === null
Alan Storm

Based on squint's comments above, I've been able to get to the bottom of this. My unstated, incorrect (and 10+ year) assumption was that the global Object helper object's prototype object was also the top level "prototype of prototypes" at the top/end of javascript's prototype chain. This is not true.

The Object helper object and the Function helper object both have the same parent prototype-object

console.log( Object.__proto__ === Function.__proto__ );    //true

So, the reason Object.__proto__ points to an empty function is -- that empty function is its prototype object for the Object object. If you want to get to the prototype of prototypes from Object (without using .prototype), you need to dig back a bit further.

console.log( Object.__proto__.__proto__ === Object.prototype );         //true

I also put together a quick diagram that maps out the real prototypes of a few of Javascript's lower level helper/constructor objects.

Finally -- I also also discovered Google Chrome has implemented the Reflect object, which includes a getPrototypeOf method, which appears to be the same as the Object.getPrototypeOf method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!