Reputation: 8030
Object.prototype
=> {}
Object.getOwnPropertyNames(Object.prototype)
=> [ 'constructor',
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'__defineGetter__',
'__lookupGetter__',
'__defineSetter__',
'__lookupSetter__',
'__proto__' ]
var x = {foo:2}
x
=> { foo: 2 }
Object.getOwnPropertyNames(x)
=> [ 'foo' ]
Why does Object.prototype
is empty but calling Object.getOwnPropertyNames(Object.prototype)
it is really not?
Upvotes: 1
Views: 76
Reputation: 1074989
It's probably down to the fact that all of Object.prototype
's properties are non-enumerable. It'll vary by console, but apparently the console you're using isn't showing non-enumerable properties when you ask it to show you Object.prototype
. In contrast, getOwnPropertyNames
returns an array containing the names of all "own" properties of the object you pass in whose names are strings, regardless of whether they're enumerable; your console is then showing you the contents of that array. (If you used Object.keys(Object.prototype)
instead, you'd get an empty array, because Object.keys
only gives you the names of enumerable properties.)
In your x
example, the object has an enumerable foo
property. I suspect if you did this:
var x = {};
Object.defineProperty(x, "foo", {value: 2});
x
...in your console, you'd see {}
like you do for Object.prototype
, because that would define foo
as a non-enumerable property.
Upvotes: 2