What is in Object .__ proto__?

In Google Chrom JavaScript, javascript objects have a property called __proto__ that points to their prototype (or parent) object.

 var foo = {}; console.log(foo.__proto__ === Object.prototype); //returns true 

However, this is not true for an Object .

 console.log(Object.__proto__ === Object.prototype); //returns false 

The Object.__proto__ looks like an empty method

 > console.log(Object.__proto__.toString()); function () {} 

Besides serving as a warning story on how to rely on javascript functions that start outside of standard bodies - what is the Object.__proto__ ?

+6
source share
2 answers

Based on the comments of the pigtails above, I was able to figure it out. My unspecified, incorrect (and more than 10 years) assumption was that the global prototype of the Object was also a prototype of the top-level prototypes at the top / end of the javascript prototype chain. It is not true.

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

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

So the reason Object.__proto__ points to an empty function: this empty function - its prototype of the object for the Object . If you want to go on to prototype prototypes from Object (without using .prototype ), you need to dig a little back.

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

I also put together a short diagram that displays real-life prototypes of several Javascript helper / constructor objects.

enter image description here

Finally, I also found that Google Chrome implemented a Reflect object, which includes the getPrototypeOf method , which appears to be the same as the Object.getPrototypeOf method.
+2
source

The upper part of the object graph is formed in such a way as to maintain the same consistency with the expectations set elsewhere in the specification.

Necessarily, a point appears where the usual relationships of objects cannot be used because you have "finished objects".

A basic understanding of JavaScript leads us to the fact that [[Prototype]] of Object is a property of the prototype function used to create a functional Object .

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

 Object.__proto__ === Function.prototype 

Since we are at the top of the object graph and want to maintain consistency in the expected behavior, we configure [[Prototype]] of Function as Function.prototype .

 Function.__proto__ === Function.prototype 

Thus, Function instanceof Function === true provided.

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

 Function.prototype.prototype === undefined 

... and each user-defined function (except for the bold arrows) has an object in the prototype property.

Due to all of the above:

 Object.__proto__ === Function.__proto__ 

This may seem odd, but, as noted earlier, in the upper part of the object’s graph we have a limited set of candidate objects that are pointed to.

TC-39 now needs to determine what was [[Prototype]] [[Prototype]] of Object . In accordance with the foregoing, we know that [[Prototype]] of Object Function.prototype .

In a sense, we are now higher than Function.prototype in the object graph, so a special instance of Object ("prototype object") was selected for this value.

This means that the top of each prototype chain can be conveniently linked using Object.prototype .

This, of course, also meets the desirable requirement that everything "be an object."

 Object.__proto__.__proto__ === Object.prototype 

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

 Object.__proto__.__proto__.__proto__ === null 
+6
source

Source: https://habr.com/ru/post/1012819/


All Articles