TypedArray.prototype.buffer
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
buffer
はTypedArray
インスタンスのアクセサープロパティで、構築時点にこの型付き配列が参照するArrayBuffer
またはSharedArrayBuffer
を返します。
試してみましょう
// Create an ArrayBuffer with a size in bytesconst buffer = new ArrayBuffer(8);const uint16 = new Uint16Array(buffer);console.log(uint16.buffer.byteLength);// Expected output: 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); // 128console.log(arr.buffer.byteLength); // 1024console.log(arr.buffer === buffer); // true
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-get-%typedarray%.prototype.buffer |