Inspired by Lance Pollard’s comment in this question, something really weird happened and I have never seen this before…
var strangeArray = {
0:"a",
1:"b",
2:"c",
length: 0,
splice: Array.prototype.splice
}
Now, when you run it:
> strangeArray;
[] <-- you get an empty array.
> strangeArray.length;
0 <-- Holy $#!T
And now:
> strangeArray[0];
"a"
> strangeArray[1];
"b"
> strangeArray[2];
"c"
What the? “Hidden values in an array”?
First I thought because it is an object, that’s why. But object should show like this:
{}
So why is this happening? Please help, I am totally confused. Thanks.
It’s just the way consoles often interpret an Object as an Array (even if it isn’t an Array at all, as in your code).
When the console sees an object with…
.lengthproperty.spliceproperty…the console decides to display it as an Array, even if it isn’t. It has no effect on what the object actually is.
This should be no surprise. You defined it on the object.
The reason they use this approach is presumably because it’s fast, and sufficiently accurate.
Once again, just because you see
[], doesn’t mean it’s an Array. The console is not part of the language. It just has the job of displaying some useful information.