https://jsfiddle.net/AleKS/ydsu1ema/4/
function constObj(number, name, drink) {
this.number = number;
this.aName = name;
this.drink = drink;
}
var obj = new constObj(2, "Jon", "Water");
console.log(obj);
console.log(obj[Object.keys(obj)[0]]);
I am learning about constructor functions at the moment and decided to test something in jsFiddle.
I noticed the console logging obj, displays object properties in alphabetical order.
Then I used Object.keys() to access a property by index.
As per console.log, I expected index 0 to be "Jon", instead it gave me "2", which is the order in which the constructor function was declared.
In the browser console, it logs obj as declared, but when you expand the object, the same alphabetical order is displayed.
It is really confusing.
Why does that happen and if I access a property through an index, can I be always sure that the initial property position would be used ?