ArrayBuffer.prototype.byteLength

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

byteLength 訪問器屬性,是 ArrayBuffer 例項的屬性,用於返回此 ArrayBuffer 的長度(以位元組為單位)。

試一試

// Create an ArrayBuffer with a size in bytes
const buffer = new ArrayBuffer(8);

// Use byteLength to check the size
const bytes = buffer.byteLength;

console.log(bytes);
// Expected output: 8

描述

byteLength 屬性是一個訪問器屬性,其 set 訪問器函式為 undefined,這意味著您只能讀取此屬性。該值在陣列構造時設定,並且無法更改。如果此 ArrayBuffer 已被分離,則此屬性將返回 0。

示例

使用 byteLength

js
const buffer = new ArrayBuffer(8);
buffer.byteLength; // 8

規範

規範
ECMAScript® 2026 語言規範
# sec-get-arraybuffer.prototype.bytelength

瀏覽器相容性

另見