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?