My understanding on the concept: objects come with a hidden property called __proto__
that provides reference to function object's prototype property. When I create an empty object and log it into the console, I see the property within the object is [[Prototype]]
instead of __proto__
. Can you provide any insight or correction to my understanding on the concept?
[[Prototype]]
refers to an internal slot of objects - values which are held in the JS engine, but not directly exposed to the running JavaScript. See here for details from the specification.
The value contained in an internal slot can often be retrieved if there are methods that deliberately expose it.
For another example of an internal slot, consider the values of a Set:
const set = new Set();
set.add('someValue');
The someValue
is not directly visible on the Set, but it can be accessed through methods which run native code, and that native code accesses the internal slots of the Set to see what it contains - for example, with Set.prototype.has
.
In the case of [[Prototype]]
, the value in that internal slot can be retrieved by invoking the __proto__
getter, which does:
- Return ?
O.[[GetPrototypeOf]]()
.
eventually accessing and returning the [[Prototype]]
internal slot.
Also, __proto__
is deprecated - you should prefer to use Object.getPrototypeOf
and Object.setPrototypeOf
.