An enumerated property is a property that can be included and visited during for..in (or a similar iteration of properties, such as Object.keys() ).
If the property is not identified as enumerable, the loop ignores it in 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"
A property is identified as enumerable or not by its own attribute [[Enumerable]] . You can view this as part of the property 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 }
for..in loop for..in executes the object property names.
var foo = { bar: 1, baz: 2}; for (var prop in foo) console.log(prop); // outputs 'bar' and 'baz'
But, only evaluates his statement - console.log(prop); in this case, for those properties whose [[Enumerable]] attribute is true .
This condition is satisfied because objects have much 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 in the object :
console.log('constructor' in foo); // true console.log('toString' in foo); // true // etc.
But they are skipped for..in because they are not listed.
var descriptor = Object.getOwnPropertyDescriptor(Object.prototype, 'constructor'); console.log(descriptor.enumerable);