Despite going through various posts and documentation, I am still puzzled about the exact definition of an enumerable property. Let me illustrate my confusion:
I have designed a constructor function and added a prototype to it.
var myAlphabet = function() {
this.c = 5
this.d = 6
}
myAlphabet.prototype = {
e:7
}
Next, I create a new instance of myAlphabet using the new keyword
var myObject = new myAlphabet();
Using a for-in loop, my intention is to display all the keys in the instance of myObject (excluding keys from the prototype).
for( key in myObj){
console.log(key);
}
This results in the following output:
'c'
'd'
'e'
Based on the information provided in the for..in loop documentation:
The for..in statement iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.
This leads me to believe that the prototype is considered an enumerable property
. However, while reading about Enumerable properties
, I came across the following explanation:
Ownership of properties is determined by whether the property belongs to the object directly and not to its prototype chain.
Since the prototype created earlier does not directly belong to the instance of myObject but is part of the prototype chain, I wonder why it gets included when looping over each key?