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

View in English Always switch to English

Uint16Array() コンストラクター

Baseline
広く利用可能

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

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

構文

js
new Uint16Array()
new Uint16Array(length)
new Uint16Array(typedArray)
new Uint16Array(object)

new Uint16Array(buffer)
new Uint16Array(buffer, byteOffset)
new Uint16Array(buffer, byteOffset, length)

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

引数

TypedArrayを参照してください。

例外

TypedArrayを参照してください。

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

js
// 長さから
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

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

// 他の TypedArray から
const y = new Uint16Array(x);
console.log(y[0]); // 21

// ArrayBuffer から
const buffer = new ArrayBuffer(16);
const z = new Uint16Array(buffer, 2, 4);
console.log(z.byteOffset); // 2

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

仕様書

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

ブラウザーの互換性

関連情報