Int32Array
Int32Array 型別化陣列表示平臺位元組序的 32 位有符號整數陣列。如果需要控制位元組序,請改用 DataView。除非顯式提供初始化資料,否則其內容將初始化為 0。建立後,您可以使用物件的 方法 或標準陣列索引語法(即使用方括號表示法)來引用陣列中的元素。
Int32Array 是隱藏的 TypedArray 類的子類。
建構函式
Int32Array()-
建立一個新的
Int32Array物件。
靜態屬性
也繼承了其父類 TypedArray 的靜態屬性.
Int32Array.BYTES_PER_ELEMENT-
返回元素大小的數值。對於
Int32Array為4。
靜態方法
繼承了其父類 TypedArray 的靜態方法.
例項屬性
也繼承了其父類 TypedArray 的例項屬性.
這些屬性定義在 Int32Array.prototype 上,並由所有 Int32Array 例項共享。
Int32Array.prototype.BYTES_PER_ELEMENT-
返回元素大小的數值。對於
Int32Array為4。 Int32Array.prototype.constructor-
建立例項物件的建構函式。對於
Int32Array例項,初始值為Int32Array建構函式。
例項方法
繼承了其父類 TypedArray 的例項方法.
示例
建立 Int32Array 的不同方法
js
// From a length
const int32 = new Int32Array(2);
int32[0] = 42;
console.log(int32[0]); // 42
console.log(int32.length); // 2
console.log(int32.BYTES_PER_ELEMENT); // 4
// From an array
const x = new Int32Array([21, 31]);
console.log(x[1]); // 31
// From another TypedArray
const y = new Int32Array(x);
console.log(y[0]); // 21
// From an ArrayBuffer
const buffer = new ArrayBuffer(32);
const z = new Int32Array(buffer, 4, 4);
console.log(z.byteOffset); // 4
// From an iterable
const iterable = (function* () {
yield* [1, 2, 3];
})();
const int32FromIterable = new Int32Array(iterable);
console.log(int32FromIterable);
// Int32Array [1, 2, 3]
規範
| 規範 |
|---|
| ECMAScript® 2026 語言規範 # sec-typedarray-objects |
瀏覽器相容性
載入中…