What is enumerable (enumerable) in a JS object?

Source: Internet
Author: User

When it comes to enumerations, it's possible that many people think of enum types, but there is a property in the JavaScript object that is enumerable, what is he?

Concept

The enumerable (enumerable) is used to control which properties are described and whether they will be included in the for...in loop. Specifically, if the enumerable of a property is false, the following three operations do not take the property.
* For: In loop
* Object.keys method
* Json.stringify method

Enumerable "invisibility"
 var o = {a:1, B:2};o.c = 3; Object.defineproperty (o,  ' d ', {value: 4, Enumerable: false}), O.d//4for ( var key in o) console.log (O[key] ); //1//2//3 object.keys (o) //["a", "B", "C"] json.stringify (o //= "{a:1,b:2,c:3}"    

In the above code, the D property is enumerable false , so the normal traversal operation cannot get the property, making it a bit like a "secret" property, but still can get its value directly.

for...in Object.keys the difference between loops and methods is that the former includes the object inherits from the 原型对象的 property, and the latter only includes the object 本身的 properties. If you need to get all the properties of the object itself, regardless of the value of enumerable, you can use the Object.getOwnPropertyNames method

What is enumerable (enumerable) in a JS object?

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.