Javascript 简明教程

JavaScript - DataView

DataView 是 JavaScript 中的一个对象,允许您处理存储在 DataView 的二进制数据。它提供了一个低级接口,用于在二进制 DataView 中读写数字类型。

The DataView is an object in JavaScript that allows you to work with the binary data stored in the ArrayBuffer. It provides a low-level interface for reading and writing number types in a binary ArrayBuffer.

DataView 对象提供了用于从缓冲区中读取和写入 1、2 和 4 字节带符号和不带符号整数以及 4 和 8 字节浮点数的内置方法。

The DataView object provides built-in methods for reading and writing 1, 2, and 4-byte signed and unsigned integers, as well as 4 and 8-byte floating-point numbers from the buffer.

Syntax

以下是 JavaScript 中创建 DataView 对象的语法 −

Following is the syntax to create a DataView object in JavaScript −

new DataView(buffer, byteOffset, byteLength)

此处, buffer 是用于存储的现有 DataViewbyteOffset 参数(可选)表示缓冲区中第一个字节的字节偏移量, byteLength 参数(也为可选)表示字节数组中的元素数量。

Here, the buffer is an existing ArrayBuffer used for storage. The byteOffset parameter (optional) represents the offset in bytes to the first byte in the buffer, and the byteLength parameter (also optional) represents the number of elements in the byte array.

Example : Creating a DataView Object

以下示例演示如何在 JavaScript 中创建 DataView 对象。

The following example demonstrates how to create a DataView object in JavaScript.

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   //creating dataview object
   const data_view = new DataView(buffer);
   document.write("The type of data_view is: " + typeof(data_view));
</script>
</body>
</html>

Output

以上程序显示了 DataView 的类型 −

The above program displays the type of the data_view as −

The type of data_view is: object

JavaScript DataView Properties

以下是 DataView 对象的属性列表 −

Here is a list of the properties of DataView object −

  1. buffer − It returns the ArrayBuffer of SharedArrayBuffer.

  2. byteLength − It returns the length (in bytes) of this view.

  3. byteOffset − It returns the offset (in bytes) of this view from the start of its ArrayBuffer or SharedArrayBuffer.

JavaScript DataView Methods

以下是在 JavaScript中 DataView 对象的方法 −

Following are the methods of the JavaScript DataView object −

Sr.No.

Methods & Description

1

getBigInt64() It returns a BigInt from the range of -263 to 263-1, included.

2

getBigUint64() It returns a BigInt from the range of 0 to 264-1, included.

3

getFloat32() It returns a floating point number from -3.4e38 to 3.4e38.

4

getFloat64() It returns any number value.

5

getInt16() It returns an integer from -32768 to 32767, included.

6

getInt32() It returns an integer from the range of -2147483648 to 2147483647, included.

7

getInt8() It returns an integer from the range of -128 to 127, included.

8

getUint16() It returns an integer from the range of 0 to 65535, included.

9

getUint32() It returns an integer from the range of 0 to 4294967295, included.

10

getUint8() It returns an integer from the range of 0 to 255, included.

11

setBigInt64() It returns undefined.

12

setBigUint64() It returns undefined.

13

setFloat32() It returns undefined.

14

setFloat64() It returns undefined.

15

setInt16() It returns undefined.

16

setInt32() It returns undefined.

17

setInt8() It returns undefined.

18

setUint16() It returns undefined.

19

setUint32() It returns undefined.

20

setUint8() It returns undefined.