このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

Uint8ClampedArray() コンストラクター

Baseline
広く利用可能

この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2015年7月以降、すべてのブラウザーで利用可能です。

Uint8ClampedArray() コンストラクターは、新しい Uint8ClampedArray オブジェクトを生成します。初期化データが明示的に指定されていない場合、内容は 0 に初期化されます。

構文

js
new Uint8ClampedArray()
new Uint8ClampedArray(length)
new Uint8ClampedArray(typedArray)
new Uint8ClampedArray(object)

new Uint8ClampedArray(buffer)
new Uint8ClampedArray(buffer, byteOffset)
new Uint8ClampedArray(buffer, byteOffset, length)

メモ: Uint8ClampedArray()new 付きでのみ構築できます。new なしで呼び出そうとすると、TypeError が発生します。

引数

TypedArrayを参照してください。

例外

TypedArrayを参照してください。

様々な方法による Uint8ClampedArray の生成

js
// 長さを指定
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

// 配列から
const x = new Uint8ClampedArray([21, 31]);
console.log(x[1]); // 31

// 他の型付き配列から
const y = new Uint8ClampedArray(x);
console.log(y[0]); // 21

// ArrayBuffer から
const buffer = new ArrayBuffer(8);
const z = new Uint8ClampedArray(buffer, 1, 4);
console.log(z.byteOffset); // 1

// 反復可能オブジェクトから
const iterable = (function* () {
  yield* [1, 2, 3];
})();
const uint8cFromIterable = new Uint8ClampedArray(iterable);
console.log(uint8cFromIterable);
// Uint8ClampedArray [1, 2, 3]

仕様書

仕様書
ECMAScript® 2027 Language Specification
# sec-typedarray-constructors

ブラウザーの互換性

関連情報