搜索 | 用户支持

防范以用户支持为名的诈骗。我们绝对不会要求您拨打电话或发送短信,及提供任何个人信息。请使用“举报滥用”选项报告涉及违规的行为。

详细了解

Array.length remains the same even though array has element index greater than length set: Bug or feature?

more options

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!

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)

more options

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 ?

more options

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.