What does enumerable mean?
2014-09-30 01:53
295 查看
I was directed to MDN's for..in page when it said, "for..in Iterates over the enumerable properties of an object."
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?
Well, whether a property is considered enumerable or not is based on its own
A
But, it only evaluates its statement --
This condition is in place because objects actually have many more properties, especially those from inheritance:
Each of these properties still exists on the object:
But, they're skipped (or "not counted") by the
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?
Well, whether a property is considered enumerable or not is based on 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 }
> var descriptor = Object.getOwnPropertyDescriptor({ bar: 1 }, 'bar'); undefined > undefined > console.log(descriptor.enumerable); // true true undefined > console.log(descriptor.value); // 1 1 undefined > undefined > console.log(descriptor); { value: 1, writable: true, enumerable: true, configurable: true } undefined > // { value: 1, writable: true, enumerable: true, configurable: true } undefined >
A
for..inloop 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'
> var foo = { bar: 1, baz: 2}; undefined > undefined > for (var prop in foo) ... console.log(prop); // outputs 'bar' and 'baz' bar baz undefined
But, it 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 actually have many more properties, especially those from inheritance:
> console.log(Object.getOwnPropertyNames(Object.prototype)); [ 'constructor', 'toString', 'toLocaleString', 'valueOf', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', '__defineGetter__', '__lookupGetter__', '__defineSetter__', '__lookupSetter__' ] undefined > // ["constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", /* etc. */]
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.
But, they're skipped (or "not counted") by the
for..inloop because they're non-enumerable.
var descriptor = Object.getOwnPropertyDescriptor(Object.prototype, 'constructor'); console.log(descriptor.enumerable); // false
相关文章推荐
- "Batch,Batch,Batch":What does it really mean?
- ${1+"$@"}是什么意思 (What does ${1+"$@"} mean)?
- What does the [Flags] Enum Attribute mean in C#?
- What does TCP Zero Window mean?
- What does it mean by rendering of page in JSF
- What does it mean by Single-Leg & Multiple Leg in Stock Trading?
- What does the first letter "c" mean in "calloc"?
- What Does SERIALIZABLE Really Mean?
- What does backend type static mean in Magento?
- What Does "Bitrate" Mean on a Video?
- What does the distance attribute in DMatches mean?
- What does $NON-NLS-1$ mean?
- What does "size" in int(size) of MySQL mean?
- In Javascript, what does this underscore mean?
- WHAT DOES “NEURONS THAT FIRE TOGETHER WIRE TOGETHER” MEAN?
- What Does “Neurons that Fire Together Wire Together” Mean?
- Build a Simple Program Using Boost in(std::cin) : What does it mean?
- what does “static int function(…) __acquires(..) __releases(…){” mean?
- What does '>/dev/null 2>&1' mean in this article of crontab basics
- What does a (+) sign mean in an Oracle SQL WHERE clause?