大网数据广告活动
Web 标准与前端开发

DataView() constructor

· 52CE 我爱测

The **`DataView()`** constructor creates DataView objects.

The DataView() constructor creates DataView objects.

JavaScript Demo: DataView() constructor

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

// Create a couple of views
const view1 = new DataView(buffer);
const view2 = new DataView(buffer, 12, 4); // From byte 12 for the next 4 bytes
view1.setInt8(12, 42); // Put 42 in slot 12

console.log(view2.getInt8(0));
// Expected output: 42

Syntax

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

[!NOTE] DataView() can only be constructed with new. Attempting to call it without new throws a TypeError.

Parameters

  • buffer
    • : An existing ArrayBuffer or SharedArrayBuffer to use as the storage backing the new DataView object.
  • byteOffset
    • : The offset, in bytes, to the first byte in the above buffer for the new view to reference. If unspecified, the buffer view starts with the first byte.
  • byteLength
    • : The number of elements in the byte array. If unspecified, the view's length will match the buffer's length.

Return value

A new DataView object representing the specified data buffer.

Exceptions

  • RangeError
    • : Thrown if the byteOffset or byteLength parameter values result in the view extending past the end of the buffer. In other words, byteOffset + byteLength > buffer.byteLength.

Examples

Using DataView

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

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

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。