TypedArray.of()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.

TypedArray.of() メソッドは、引数の変数番号から新しい型付き配列を生成します。このメソッドは Array.of() とほぼ同じです。

試してみましょう

const int16array = Int16Array.of("10", "20", "30", "40", "50");

console.log(int16array);
// Expected output: Int16Array [10, 20, 30, 40, 50]

構文

js
TypedArray.of()
TypedArray.of(element1)
TypedArray.of(element1, element2)
TypedArray.of(element1, element2, /* …, */ elementN)

ここで TypedArray は次のいずれかです。

引数

element1, …, elementN

型付き配列を作成するのに使われる要素です。

返値

新しい TypedArray のインスタンスです。

解説

詳細については、 Array.of() をご覧ください。 Array.of()TypedArray.of() との間にはいくつか微妙な違いがあります。

  • TypedArray.of() に渡された this の値がコンストラクターではなかった場合、 TypedArray.of() では TypeError が発生します。一方、 Array.of() は既定で新しい Array を生成します。
  • TypedArray.of()[[Set]] を使用するのに対し、 Array.of()[[DefineProperty]] を使用します。従って Proxy オブジェクトを使っている場合は、新しい要素を追加するときに handler.set()handler.defineProperty() の代わりに呼び出されます。

of() の使用

js
Uint8Array.of(1); // Uint8Array [ 1 ]
Int8Array.of("1", "2", "3"); // Int8Array [ 1, 2, 3 ]
Float32Array.of(1, 2, 3); // Float32Array [ 1, 2, 3 ]
Int16Array.of(undefined); // Int16Array [ 0 ]

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-%typedarray%.of

ブラウザーの互換性

関連情報