What does enumerable mean?

Source: Internet
Author: User
Tags hasownproperty

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. cocould 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); // trueconsole.log(descriptor.value);      // 1console.log(descriptor);// { value: 1, writable: true, enumerable: true, configurable: true }
> var descriptor = Object.getOwnPropertyDescriptor({ bar: 1 }, ‘bar‘);undefined>undefined> console.log(descriptor.enumerable); // truetrueundefined> console.log(descriptor.value);      // 11undefined>undefined> console.log(descriptor);{ value: 1, writable: true, enumerable: true, configurable: true }undefined> // { value: 1, writable: true, enumerable: true, configurable: true }undefined>

Afor..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‘barbazundefined

But, it only evaluates itsStatement--console.log(prop);In this case -- for those properties whose[[Enumerable]]Attribute istrue.

This condition is in place because objects actually have has 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); // trueconsole.log(‘toString‘ in foo);    // true// etc.

But, they're skipped (or"Not counted") Byfor..inLoop because they're non-enumerable.

var descriptor = Object.getOwnPropertyDescriptor(Object.prototype, ‘constructor‘);console.log(descriptor.enumerable); // false

 

What does enumerable mean?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.