Array.length remains the same even though array has element index greater than length set: Bug or feature?
I don't really know if this is a bug, or a feature of Firefox's JavaScript core engine. Say I have an array:
var array = [];
I set the length of the array to a very large number:
array.length = 155555555;
This now turns the variable array into an array with 155555555 blank slots. If I then do:
array[502039410402] = 5; // Notice that 502039410402 > the previously set 155555555 for length
array.length remains at 155555555, but the actual array is appended with a sudden index 502039410402 (i.e., index 155555553, 155555554, and then suddenly index 502039410402; see screenshot), with that element containing 5. Is this a bug, or an intended feature?
I would appreciate it if anyone could let me know.
Thanks!
כל התגובות (2)
The values you use are very large.
Can you replicate this with small values like 50 and 100 and if not, when does it stop working ?
Oh no, array.length isn't read-only? Why, JavaScript, why?! But setting that aside...
The MDN article says length must be less than 232 (4294967296).
Elements with indexes beyond that size seem to exist in a state of limbo, and don't behave like members of the array in array operations:
I've read many times that everything in JavaScript is an object, so perhaps that's why the limbo elements can exist.