RangeError:无效的数组长度

🏰 必定赢365线路检测 📅 2025-10-12 06:48:08 👤 admin 👁️ 9004 👑 328
RangeError:无效的数组长度

RangeError:无效的数组长度当指定数组长度为负数、浮点数或超过平台支持的最大值时,会发生 JavaScript 异常 "无效数组长度"(例如,创建 Array 或 ArrayBuffer,或设置 length 属性)。

允许的最大数组长度取决于平台、浏览器和浏览器版本。对于 Array,最大长度为 232-1。对于 ArrayBuffer,在 32 位系统上,最大值为 231-1 (2GiB-1)。从 Firefox 89 版本开始,ArrayBuffer 的最大值为 64 位系统上的 233 (8GiB)。

注意: Array 和 ArrayBuffer 是独立的数据结构(一个的实现不会影响另一个)。

消息RangeError: invalid array length (V8-based & Firefox)

RangeError: Array size is not a small enough positive integer. (Safari)

RangeError: Invalid array buffer length (V8-based)

RangeError: length too large (Safari)

错误类型RangeError出了什么问题?当尝试使用无效长度创建 Array 或 ArrayBuffer 时,可能会出现错误,包括

负长度,通过构造函数或设置 length 属性。

非整数字符串长度,通过构造函数或设置 length 属性。(ArrayBuffer 构造函数会将长度强制转换为整数,但 Array 构造函数不会。)

超过平台支持的最大长度。对于数组,最大长度为 232-1。对于 ArrayBuffer,在 32 位系统上,最大长度为 231-1 (2GiB-1),在 64 位系统上为 233 (8GiB)。这可以通过构造函数、设置 length 属性或隐式设置 length 属性的数组方法(例如 push 和 concat)发生。

如果使用构造函数创建 Array,则可能需要使用文字表示法,因为第一个参数被解释为 Array 的长度。否则,可能需要在设置长度属性之前对长度进行限制,或将其用作构造函数的参数。

例子无效情况jsnew Array(Math.pow(2, 40));

new Array(-1);

new ArrayBuffer(Math.pow(2, 32)); // 32-bit system

new ArrayBuffer(-1);

const a = [];

a.length = a.length - 1; // set the length property to -1

const b = new Array(Math.pow(2, 32) - 1);

b.length = b.length + 1; // set the length property to 2^32

b.length = 2.5; // set the length property to a floating-point number

const c = new Array(2.5); // pass a floating-point number

// Concurrent modification that accidentally grows the array infinitely

const arr = [1, 2, 3];

for (const e of arr) {

arr.push(e * 10);

}

有效情况js[Math.pow(2, 40)]; // [ 1099511627776 ]

[-1]; // [ -1 ]

new ArrayBuffer(Math.pow(2, 31) - 1);

new ArrayBuffer(Math.pow(2, 33)); // 64-bit systems after Firefox 89

new ArrayBuffer(0);

const a = [];

a.length = Math.max(0, a.length - 1);

const b = new Array(Math.pow(2, 32) - 1);

b.length = Math.min(0xffffffff, b.length + 1);

// 0xffffffff is the hexadecimal notation for 2^32 - 1

// which can also be written as (-1 >>> 0)

b.length = 3;

const c = new Array(3);

// Because array methods save the length before iterating, it is safe to grow

// the array during iteration

const arr = [1, 2, 3];

arr.forEach((e) => arr.push(e * 10));

另请参阅

Array

length

ArrayBuffer

皇家推荐

徽章定制为什么那么贵?
体育365地址

徽章定制为什么那么贵?

📅 07-18 👁️ 4224
怪物猎人世界角龙位置在哪里
体育365地址

怪物猎人世界角龙位置在哪里

📅 08-22 👁️ 6357
关于 @icloud.com、@me.com 和 @mac.com 电子邮件地址
必定赢365线路检测

关于 @icloud.com、@me.com 和 @mac.com 电子邮件地址

📅 08-06 👁️ 1747
深度拆解“AIGC第一股”出门问问
必定赢365线路检测

深度拆解“AIGC第一股”出门问问

📅 10-03 👁️ 8304
如何关闭代理IP设置的有效途径
365bet体育线上投注

如何关闭代理IP设置的有效途径

📅 10-04 👁️ 5127
Python这么火,为什么说它不是未来的编程语言?
365bet体育线上投注

Python这么火,为什么说它不是未来的编程语言?

📅 09-25 👁️ 9580