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

浏览器兼容性

另见