I was playing with the method Object.defineProperty() and the enumerable
property of the descriptor
argument. On the MDN
you can read the next description:
enumerable:
true
if and only if this property shows up during enumeration of the properties on the corresponding object. Defaults tofalse
.
However, I'm wonder about the reason it don't affect methods that traverse iterable objects, like the for ... of. On the next example you can see the comparison between traversing the array using for ... of
and for ... in
.
let arr = [1, 2, 3, 4, 5];
Object.defineProperty(arr, "4", {value: 99, enumerable: false});
console.log("For ... of traverse non-enumerable properties:");
for (const ele of arr)
{
console.log(ele);
}
console.log("For ... in don't traverse non-enumerable properties:");
for (const key in arr)
{
console.log(arr[key]);
}
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
Also, as the title of this question mention, built-in
array methods ignores this setting too, but object
methods don't:
let arr = [1, 2, 3, 4, 5];
Object.defineProperty(arr, "4", {value: 99, enumerable: false});
console.log("forEach(): ");
arr.forEach(x => console.log(x));
console.log("map(): ", arr.map(x => x + 1));
console.log("reduce(): ", arr.reduce((acc, x) => `${acc + x},` , ""));
console.log("Object.keys(): ", Object.keys(arr));
console.log("Object.values(): ", Object.values(arr));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
I'm not saying this is wrong or unexpected behavior, but just looking for an explanation of this situation, thanks!