MDN states:
Also, when iterating over the properties of an object, every enumerable property that is on the prototype chain will be enumerated.
So I tried this:
var x = {a: "I am a"};
var z = Object.create(x);
for( i in z )
{
console.dir( i );
if( i == "hasOwnProperty" ) {
console.log( 'found hasOwnProperty' );
}
}
Outputs only a but not hasOwnProperty. Why?
Because Object.prototype.hasOwnProperty is non-enumerable:
Object.getOwnPropertyDescriptor(Object.prototype, 'hasOwnProperty')
.enumerable // false
Therefore, it's not iterated by the for...in loop.
Because hasOwnProperty is not enumerable , you can test it using
console.log(Object.getOwnPropertyDescriptor(Object.prototype, "hasOwnProperty").enumerable)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With