When using square brackets around __proto__
in a object, __proto__
is enumerable. When no square brackets are provided, __proto__
is not enumerable.
Example:
obj = {"__proto__": "Hello"}
for (var k in obj)
{
console.log(k)
};
// No Output
Using Square Brackets
obj = {["__proto__"]: "Hello"}
for (var k in obj)
{
console.log(k)
};
// Output:
// __proto__
I understand that using the square brackets computed property names, but I don't understand why one would be enumerable and another would not.
This is described in the specification. Computed property names of __proto__
are specifically excluded from the isProtoSetter
check, whereas normal string values of __proto__
are permitted:
- If propKey is the String value
__proto__
and if IsComputedPropertyKey(PropertyName) is false, then
a. Let isProtoSetter be true.
- Else,
a. Let isProtoSetter be false.
The isProtoSetter
flag, when true, indicates that the created object should have an internal prototype of the value paired with that key. The __proto__
for the standard internal prototype of an object is not enumerable - it exists on Object.prototype
:
console.log(Object.getOwnPropertyDescriptor(Object.prototype, '__proto__'));
When false, it's equivalent to having a plain property which is named __proto__
, and plain properties in object initializers become enumerable.