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