Now that we know that o
is an Array, I can answer you precisely.
No, the elements will not be 'allocated' or 'created'.
When you make an assignment of an index property to an Array object, which is greater than the actual length
of the array two things happen:
- The index named property is created
- The
length
property is incremented, to be the index + 1
For example:
var o = [];
o[4000] = true;
o.hasOwnProperty(0); // false, the property doesn't exist
o.hasOwnProperty(1); // false
o.hasOwnProperty(4000); // true, the property exist
As you can see, the hasOwnProperty
method returns false
when we test the presence of the 0
or 1
properties, because they don't exist physically on the object, whereas it returns true
for 4000
, the property that was created.
When Firebug detects that the object being printed in the console is an array-like object, it will simply make a loop, showing each of the index values from 0
to length - 1
.
Firebug detects array-like objects simply by looking if they have a length
property whose its value is an unsigned 32-bit integer (less than 2^32 - 1
), and if they have a splice
property that is a function, for example, the following object will be detected and printed as an Array on the Firebug's console:
console.log({length:3, splice:function(){}});
// Firebug will log: `[undefined, undefined, undefined]`