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?
为什么Object.prototype是空的但调用Object.getOwnPropertyNames(Object.prototype)它真的不是?
1 个解决方案
#1
2
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.)
这可能是因为Object.prototype的所有属性都是不可枚举的。它会因控制台而异,但是当你要求它向你展示Object.prototype时,显然你正在使用的控制台没有显示非可枚举的属性。相比之下,getOwnPropertyNames返回一个数组,其中包含您传入的对象的所有“自有”属性的名称,无论它们是否可枚举;然后你的控制台会显示该数组的内容。 (如果您使用了Object.keys(Object.prototype),那么您将得到一个空数组,因为Object.keys只为您提供了可枚举属性的名称。)
In your x
example, the object has an enumerable foo
property. I suspect if you did this:
在x示例中,该对象具有可枚举的foo属性。我怀疑你是否这样做:
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.
...在你的控制台中,你会看到{}就像你为Object.prototype做的那样,因为这会将foo定义为不可枚举的属性。
#1
2
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.)
这可能是因为Object.prototype的所有属性都是不可枚举的。它会因控制台而异,但是当你要求它向你展示Object.prototype时,显然你正在使用的控制台没有显示非可枚举的属性。相比之下,getOwnPropertyNames返回一个数组,其中包含您传入的对象的所有“自有”属性的名称,无论它们是否可枚举;然后你的控制台会显示该数组的内容。 (如果您使用了Object.keys(Object.prototype),那么您将得到一个空数组,因为Object.keys只为您提供了可枚举属性的名称。)
In your x
example, the object has an enumerable foo
property. I suspect if you did this:
在x示例中,该对象具有可枚举的foo属性。我怀疑你是否这样做:
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.
...在你的控制台中,你会看到{}就像你为Object.prototype做的那样,因为这会将foo定义为不可枚举的属性。