__proto__
is a property of Object.prototype
:
Object.prototype.hasOwnProperty('__proto__'); // true
However, getOwnPropertyNames ignores it on Firefox:
Object.getOwnPropertyNames(Object.prototype).indexOf('__proto__'); // -1
It works on Chromium 43 and IE 11, tough.
AFAIK, according to ECMAScript 5 spec, getOwnPropertyNames should list it:
15.2.3.4 - Object.getOwnPropertyNames ( O )
When the getOwnPropertyNames function is called, the following steps are taken:
- If Type(O) is not Object throw a TypeError exception.
- Let array be the result of creating a new object as if by the expression
new Array ()
whereArray
is the standard built-in constructor with that name.- Let n be 0.
- For each named own property P of O
- Let name be the String value that is the name of P.
- Call the [[DefineOwnProperty]] internal method of array with arguments ToString(n), the PropertyDescriptor {[[Value]]: name, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, and false.
- Increment n by 1.
- Return array.
__proto__
is a named own property of Object.prototype
(otherwise, hasOwnProperty would return false because [[GetOwnProperty]] would return undefined). Therefore, it should be listed by getOwnPropertyNames, shouldn't it?
Should that be different in ECMAScript 6? The ES6 draft changes a few things indeed:
19.1.2.7 - Object.getOwnPropertyNames ( O )
When the getOwnPropertyNames function is called, the following steps are taken:
- Return GetOwnPropertyKeys(O, String).
19.1.2.8.1 - Runtime Semantics: GetOwnPropertyKeys ( O, Type )
The abstract operation GetOwnPropertyKeys is called with arguments O and Type where O is an Object and Type is one of the ECMAScript specification types String or Symbol. The following steps are taken:
- Let obj be ToObject(O).
- ReturnIfAbrupt(obj).
- Let keys be obj.[OwnPropertyKeys].
- ReturnIfAbrupt(keys).
- Let nameList be a new empty List.
- Repeat for each element nextKey of keys in List order,
- If Type(nextKey) is Type, then
- Append nextKey as the last element of nameList.
- Return CreateArrayFromList(nameList).
9.1.12 - [[OwnPropertyKeys]] ( )
When the [[OwnPropertyKeys]] internal method of O is called the following steps are taken:
- Let keys be a new empty List.
- For each own property key P of O that is an integer index, in ascending numeric index order
- Add P as the last element of keys.
- For each own property key P of O that is a String but is not an integer index, in property creation order
- Add P as the last element of keys.
- For each own property key P of O that is a Symbol, in property creation order
- Add P as the last element of keys.
- Return keys.
[[OwnPropertyKeys]] should return a list which contains "__proto__"
, and GetOwnPropertyKeys should return an array with "__proto__"
, because its type should be String.
And even in case it was Symbol instead of String (which doesn't make much sense), getOwnPropertySymbols should include __proto__
, but it returns an empty array.
Therefore, is it a bug, or what intricacy of the spec applies here?