此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

TypedArray.prototype.length

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

TypedArray 实例的 length 访问器属性返回该类型化数组的长度(以元素为单位)。

尝试一下

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

console.log(uint8.length);
// Expected output: 6

描述

length 属性是一个 set 访问器函数为 undefined 的访问器属性,这意味着你只能读取该属性。该值在构建 TypedArray 时确定,不可更改。如果 TypedArray 没有指定 byteOffsetlength,则将返回引用的 ArrayBuffer 的长度。TypedArrayTypedArray 对象之一。

示例

使用 length 属性

js
const buffer = new ArrayBuffer(8);

let uint8 = new Uint8Array(buffer);
uint8.length; // 8(与缓冲区 length 相匹配)

uint8 = new Uint8Array(buffer, 1, 5);
uint8.length; // 5(在构造 Uint8Array 时指定)

uint8 = new Uint8Array(buffer, 2);
uint8.length; // 6(根据被构造的 Uint8Array 的偏移量)

规范

Specification
ECMAScript® 2026 Language Specification
# sec-get-%typedarray%.prototype.length

浏览器兼容性

参见