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 July 2015.

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

尝试一下

描述

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 Language Specification
# sec-get-%typedarray%.prototype.length

浏览器兼容性

BCD tables only load in the browser

参见