DataView.prototype.byteLength
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
byteLength
アクセサープロパティは、このビューのArrayBuffer
またはSharedArrayBuffer
の先頭から長さを (バイト単位で) 表します。
試してみましょう
// Create an ArrayBuffer with a size in bytesconst buffer = new ArrayBuffer(16);const view1 = new DataView(buffer);const view2 = new DataView(buffer, 12, 4); // From byte 12 for the next 4 bytesconsole.log(view1.byteLength + view2.byteLength); // 16 + 4// Expected output: 20
解説
byteLength
プロパティは、設定アクセサー関数がundefined
である、すなわち読み取りのみができるアクセサープロパティです。この値はDataView
が構築されたときに確立され、変更することができません。DataView
がオフセットやbyteLength
を指定していなかった場合は、参照されているArrayBuffer
またはSharedArrayBuffer
のbyteLength
が返されます。
例
byteLength プロパティの使用
js
var buffer = new ArrayBuffer(8);var dataview = new DataView(buffer);dataview.byteLength; // 8 (matches the byteLength of the buffer)var dataview2 = new DataView(buffer, 1, 5);dataview2.byteLength; // 5 (as specified when constructing the DataView)var dataview3 = new DataView(buffer, 2);dataview3.byteLength; // 6 (due to the offset of the constructed DataView)
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-get-dataview.prototype.bytelength |