此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

BigInt64Array() 构造函数

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2021年9月⁩.

BigInt64Array() 构造函数用于创建 BigInt64Array 对象。除非显式提供初始化数据,否则其内容会被初始化为 0n

语法

js
new BigInt64Array()
new BigInt64Array(length)
new BigInt64Array(typedArray)
new BigInt64Array(object)

new BigInt64Array(buffer)
new BigInt64Array(buffer, byteOffset)
new BigInt64Array(buffer, byteOffset, length)

备注: BigInt64Array() 只能通过 new 来构造。如果尝试在没有使用 new 的情况下调用它,会抛出 TypeError 异常。

参数

参见 TypedArray

异常

参见 TypedArray

示例

创建 BigInt64Array 的不同方式

js
// 通过长度创建
const bigint64 = new BigInt64Array(2);
bigint64[0] = 42n;
console.log(bigint64[0]); // 42n
console.log(bigint64.length); // 2
console.log(bigint64.BYTES_PER_ELEMENT); // 8

// 通过数组创建
const x = new BigInt64Array([21n, 31n]);
console.log(x[1]); // 31n

// 通过另一个 TypedArray 创建
const y = new BigInt64Array(x);
console.log(y[0]); // 21n

// 通过 ArrayBuffer 创建
const buffer = new ArrayBuffer(64);
const z = new BigInt64Array(buffer, 8, 4);
console.log(z.byteOffset); // 8

// 通过可迭代对象创建
const iterable = (function* () {
  yield* [1n, 2n, 3n];
})();
const bigint64FromIterable = new BigInt64Array(iterable);
console.log(bigint64FromIterable);
// BigInt64Array [1n, 2n, 3n]

规范

Specification
ECMAScript® 2026 Language Specification
# sec-typedarray-constructors

浏览器兼容性

参见