DataView() 构造函数

DataView() 构造函数创建 DataView 对象。

¥The DataView() constructor creates DataView objects.

Try it

语法

¥Syntax

js
new DataView(buffer)
new DataView(buffer, byteOffset)
new DataView(buffer, byteOffset, byteLength)

注意:DataView() 只能与 new 一起构建。尝试在没有 new 的情况下调用它会抛出 TypeError

¥Note: DataView() can only be constructed with new. Attempting to call it without new throws a TypeError.

参数

¥Parameters

buffer

现有的 ArrayBufferSharedArrayBuffer 用作支持新 DataView 对象的存储。

byteOffset Optional

到上述缓冲区中供新视图引用的第一个字节的偏移量(以字节为单位)。如果未指定,缓冲区视图从第一个字节开始。

byteLength Optional

字节数组中的元素数量。如果未指定,视图的长度将与缓冲区的长度匹配。

返回值

¥Return value

代表指定数据缓冲区的新 DataView 对象。

¥A new DataView object representing the specified data buffer.

例外情况

¥Exceptions

RangeError

如果 byteOffsetbyteLength 参数值导致视图超出缓冲区末尾,则抛出该异常。换句话说,byteOffset + byteLength > buffer.byteLength

示例

¥Examples

使用数据视图

¥Using DataView

js
const buffer = new ArrayBuffer(16);
const view = new DataView(buffer, 0);

view.setInt16(1, 42);
view.getInt16(1); // 42

规范

Specification
ECMAScript Language Specification
# sec-dataview-constructor

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看