Why can a prototype be restored, but __proto__ undefined in JavaScript?

Now I am learning JavaScript prototype and __proto__ and find some useful links

__ proto__ VS. JavaScript prototype

How is __proto__ different from the .prototype constructor?

I can get the __proto__ value of the __proto__ object in the following codes in Chrome.

 var Foo = function() {} var f = new Foo(); f.__proto__ > Foo {} 

However, after setting Foo.prototype.__proto__ to null , __proto__ is undefined .

 var Foo = function() {} Foo.prototype = {name: 'cat', age: 10}; Foo.prototype.__proto__ = null; var f = new Foo(); f.__proto__ > undefined 

But I can get the value of f.name , which is cat . Here is my understanding, since the value of f.name can be restored, the __proto__ of f should point to Foo.prototype . Why is the value of f.__proto__ equal to undefined ?

+2
javascript google-chrome prototype v8
source share
2 answers

According to the ES2015 specification , __proto__ is an accessor property that is inherited from Object.prototype .

Since your prototype chain for instance f embedded in null , not Object.prototype , object f does not inherit any properties from Object.prototype , including Object.prototype.__proto__ .

The object still knows its prototype internally (via the internal [[Prototype]] slot), but it does not inherit the accesster __proto__ property to obtain this value. You can still access it through Object.getPrototypeOf(f) .

See also the resolution on the Chromium obj.__proto__ is undefined problem if the prototype chain does not contain Object.prototype ":

This works as directed. ES6 __proto__ is a getter defined in Object.prototype. For an object that does not have this in its prototype chain, it is unavailable (as, for example, hasOwnProperty is not). Instead, you need to use Object.getPrototypeOf.

+3
source share

__proto__ is an internal special property of JavaScript. you should not use.

From mdn

While Object.prototype. Proto is supported today in most browsers, its existence and exact behavior were standardized only in the ECMAScript 6 specification as an obsolete feature for ensuring compatibility for web browsers. For best support, it is recommended that you use Object.getPrototypeOf () instead.

+5
source share

All Articles