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