One additional answer on the behavior of console.log
. Plain simple, the output is sugar and technically wrong.
Lets consider this example:
var foo = new Array(4),
bar = [undefined, undefined, undefined, undefined];
console.log( Object.getOwnPropertyNames(bar) );
console.log( Object.getOwnPropertyNames(foo) );
As we can see in the result, the .getOwnPropertyNames
function returns
["length"]
for the foo
Array/Object, but
["length", "0", "1", "2", "3"]
for the bar
Array/Object.
So, the console.log
just fools you on outputting Arrays
which just have a defined .length
but no real property assignments.