Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

javascript - What is in Object.__proto__?

In Google Chrom's javascript, objects have a property named __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 the Object object.

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

The Object.__proto__ property appears to be an empty method

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

Beyond serving as a warning story about relying on javascript features that start outside standard bodies -- what is the Object.__proto__ function?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...