Skip to content Skip to sidebar Skip to footer

Why The Prototype Can Be Retrieved But The __proto__ Is Undefined In Javascript?

Now I am learning JavaScript prototype and __proto__, and find several useful links __proto__ VS. prototype in JavaScript How does __proto__ differ from constructor.prototype? I ca

Solution 1:

__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 has only been standardized in the ECMAScript 6 specification as a legacy feature to ensure compatibility for web browsers. For better support, it is recommended that only Object.getPrototypeOf() be used instead.

Solution 2:

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

Since your prototype chain for the instance f is rooted in null, rather than Object.prototype, the f object does not inherit any properties from Object.prototype, including Object.prototype.__proto__.

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

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

This is working as specified. ES6 __proto__ is a getter defined on Object.prototype. For an object that doesn't have that in its prototype chain it is not accessible (just like, say, hasOwnProperty isn't). You need to use Object.getPrototypeOf instead.

Post a Comment for "Why The Prototype Can Be Retrieved But The __proto__ Is Undefined In Javascript?"