Why do square brackets around __proto__ makes it enumerable in an object

When using square brackets around __proto__ in a object, __proto__ is enumerable. When no square brackets are provided, __proto__ is not enumerable.


obj = {"__proto__": "Hello"}
for (var k in obj)
// No Output

Using Square Brackets

obj = {["__proto__"]: "Hello"}
for (var k in obj)
// 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:

  1. If propKey is the String value __proto__ and if IsComputedPropertyKey(PropertyName) is false, then

a. Let isProtoSetter be true.

  1. 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.

Source: stackoverflow
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .