BigInt64Array
BigInt64Array
类型数组表示一个包含平台字节序中的 64 位有符号整数的数组。如果需要控制字节序,请改用 DataView
。除非显式提供初始化数据,否则其内容将初始化为 0n
。一旦建立,您就可以使用该对象的方法或使用标准的数组索引语法(即使用方括号表示法)来引用数组中的元素。
BigInt64Array
是隐藏的 TypedArray
类的子类。
试一试
const buffer = new ArrayBuffer(24);
const bigint64 = new BigInt64Array(buffer);
bigint64[0] = 5886014448488689n;
bigint64[1] = 1881938909131133n;
bigint64[2] = 1898875537769492n;
bigint64[0] = 6118793953620967n;
console.log(bigint64);
// Expected Output: BigInt64Array [6118793953620967n, 1881938909131133n, 1898875537769492n]
console.log(bigint64[2]);
// Expected Output: 1898875537769492n
console.log("Array length:", bigint64.length);
// Expected Output: Array length: 3
console.log("Array byte length:", bigint64.byteLength);
// Expected Output: Array byte length: 24
console.log("Array byte offset:", bigint64.byteOffset);
// Expected Output: Array byte offset: 0
bigint64.set([100n, 200n], 1);
console.log(bigint64);
// Expected Output: BigInt64Array [6118793953620967n, 100n, 200n]
构造函数
BigInt64Array()
-
创建一个新的
BigInt64Array
对象。
静态属性
也继承了其父类 TypedArray
的静态属性.
BigInt64Array.BYTES_PER_ELEMENT
-
返回一个表示元素大小的数值。对于
BigInt64Array
,此值为8
。
静态方法
继承了其父类 TypedArray
的静态方法.
实例属性
也继承了其父类 TypedArray
的实例属性.
这些属性定义在 BigInt64Array.prototype
上,并由所有 BigInt64Array
实例共享。
BigInt64Array.prototype.BYTES_PER_ELEMENT
-
返回一个表示元素大小的数值。对于
BigInt64Array
,此值为8
。 BigInt64Array.prototype.constructor
-
创建实例对象的构造函数。对于
BigInt64Array
实例,初始值为BigInt64Array
构造函数。
实例方法
继承了其父类 TypedArray
的实例方法.
示例
创建 BigInt64Array 的不同方法
js
// From a length
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
// From an array
const x = new BigInt64Array([21n, 31n]);
console.log(x[1]); // 31n
// From another TypedArray
const y = new BigInt64Array(x);
console.log(y[0]); // 21n
// From an ArrayBuffer
const buffer = new ArrayBuffer(64);
const z = new BigInt64Array(buffer, 8, 4);
console.log(z.byteOffset); // 8
// From an iterable
const iterable = (function* () {
yield* [1n, 2n, 3n];
})();
const bigint64FromIterable = new BigInt64Array(iterable);
console.log(bigint64FromIterable);
// BigInt64Array [1n, 2n, 3n]
规范
规范 |
---|
ECMAScript® 2026 语言规范 # sec-typedarray-objects |
浏览器兼容性
加载中…