可枚举的意思是什么? [英] What does enumerable mean?
问题描述
我被导向了MDN的 ..in page 当它说,for..in迭代一个对象的可枚举属性。
I was directed to MDN's for..in page when it said, "for..in Iterates over the enumerable properties of an object."
然后我去了属性页面的可用性和所有权,其中说可枚举的属性是可以由一个迭代的属性for..in loop。
Then I went to the Enumerability and ownership of properties page where it said "Enumerable properties are those which can be iterated by a for..in loop."
字典将可枚举定义为可数,但我无法真实地想象这意味着什么。我可以得到一个可枚举的例子吗?
The dictionary defines enumerable as countable, but I can't really visualize what that means. Could i get an example of something being enumerable?
推荐答案
可枚举的属性是可以包含在<$期间的一个属性c $ c> for..in 循环(或类似的属性迭代,如 Object.keys()
)。
An enumerable property is one that can be included in and visited during for..in
loops (or a similar iteration of properties, like Object.keys()
).
如果某个属性未被识别为可枚举,则循环将忽略它在对象内。
If a property isn't identified as enumerable, the loop will ignore that it's within the object.
var obj = { key: 'val' };
console.log('toString' in obj); // true
console.log(typeof obj.toString); // "function"
for (var key in obj)
console.log(key); // "key"
属性被识别为可枚举与否的 [[Enumerable]]
属性。您可以将其视为属性描述符<的一部分/ a>:
A property is identified as enumerable or not by its own [[Enumerable]]
attribute. You can view this as part of the property's descriptor:
var descriptor = Object.getOwnPropertyDescriptor({ bar: 1 }, 'bar');
console.log(descriptor.enumerable); // true
console.log(descriptor.value); // 1
console.log(descriptor);
// { value: 1, writable: true, enumerable: true, configurable: true }
A for..in
循环然后遍历对象的属性名称。
A for..in
loop then iterates through the object's property names.
var foo = { bar: 1, baz: 2};
for (var prop in foo)
console.log(prop); // outputs 'bar' and 'baz'
但是,只评估其声明 - console.log(prop);
在这种情况下 - 对于那些 [[Enumerable]]
属性的属性true
。
But, only evaluates its statement – console.log(prop);
in this case – for those properties whose [[Enumerable]]
attribute is true
.
This condition is in place because objects have many more properties, especially from inheritance:
console.log(Object.getOwnPropertyNames(Object.prototype));
// ["constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", /* etc. */]
这些属性中的每一个仍然存在于对象上:
Each of these properties still exists on the object:
console.log('constructor' in foo); // true
console.log('toString' in foo); // true
// etc.
但是,它们被<$跳过了c $ c> for..in 循环因为它们不可枚举。
But, they're skipped by the for..in
loop because they aren't enumerable.
var descriptor = Object.getOwnPropertyDescriptor(Object.prototype, 'constructor');
console.log(descriptor.enumerable); // false
这篇关于可枚举的意思是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!