◐ Shell
clean mode source ↗

TypedArray.prototype.buffer - JavaScript | MDN

試してみましょう

// ArrayBuffer をバイト単位のサイズで作成
const buffer = new ArrayBuffer(8);
const uint16 = new Uint16Array(buffer);

console.log(uint16.buffer.byteLength);
// 予想される結果: 8

解説

byteLength プロパティは設定アクセサープロパティが undefined である、読み取り専用のアクセサープロパティです。値は TypedArray が構築されたときに確立し、変更することができません。 TypedArray型付き配列オブジェクトのうちの一つです。

型付き配列はバッファーのビューであるため、基盤となるバッファーは型付き配列自体よりも長い場合があります。

buffer プロパティの使用

js

const buffer = new ArrayBuffer(8);
const uint16 = new Uint16Array(buffer);
uint16.buffer; // ArrayBuffer { byteLength: 8 }

配列の断片のビューから、基盤のバッファーにアクセス

js

const buffer = new ArrayBuffer(1024);
const arr = new Uint8Array(buffer, 64, 128);
console.log(arr.byteLength); // 128
console.log(arr.buffer.byteLength); // 1024
console.log(arr.buffer === buffer); // true

仕様書

仕様書
ECMAScript® 2027 Language Specification
# sec-get-%typedarray%.prototype.buffer

ブラウザーの互換性

関連情報