翻译正在进行中。

DataView 视图是一个可以从 ArrayBuffer 对象中读写多种数值类型的底层接口,使用它时,不用考虑不同平台的字节序问题。

语法

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

参数

buffer
一个 ArrayBufferSharedArrayBuffer  对象,DataView 对象的数据源。
byteOffset 可选
DataView 对象的第一个字节在 buffer 中的偏移。如果不指定则默认从第一个字节开始。
byteLength 可选
此 DataView 对象的字节长度。如果不指定则默认与 buffer 的长度相同。

返回值

一个由 buffer 生成的 DataView 对象。(That probably wasn't a very helpful description.)

You can think of the returned object as an "interpreter" of the array buffer of bytes - it knows how to convert numbers to fit within the buffer correctly, both when reading and writing to it. This means handling integer and float conversion, endianness, and other details of representing numbers in binary form.

异常

RangeError
如果由偏移(byteOffset)和字节长度(byteLength)计算得到的结束位置超出了 buffer 的长度,抛出此异常。

For example, if the buffer is 16 bytes long, the byteOffset is 8, and the byteLength is 10, this error is thrown because the resulting view tries to extend 2 bytes past the total length of the buffer.

描述

字节序

需要多个字节来表示的数值,在存储时其字节在内存中的相对顺序依据平台架构的不同而不同,参照 Endianness。而使用 DataView 的访问函数时不需要考虑平台架构中所使用的是哪种字节序。

var littleEndian = (function() {
  var buffer = new ArrayBuffer(2);
  new DataView(buffer).setInt16(0, 256, true /* 设置值时,使用小端字节序 */);
  // Int16Array 使用系统字节序(由此可以判断系统字节序是否为小端字节序)
  return new Int16Array(buffer)[0] === 256;
})();
console.log(littleEndian); // 返回 true 或 false

64-bit Integer Values

Because JavaScript does not currently include standard support for 64-bit integer values, DataView does not offer native 64-bit operations. As a workaround, you could implement your own getUint64() function to obtain a value with precision up to Number.MAX_SAFE_INTEGER, which could suffice for certain cases.

function getUint64(dataview, byteOffset, littleEndian) {
  // split 64-bit number into two 32-bit (4-byte) parts
  const left =  dataview.getUint32(byteOffset, littleEndian);
  const right = dataview.getUint32(byteOffset+4, littleEndian);

  // combine the two 32-bit values
  const combined = littleEndian? left + 2**32*right : 2**32*left + right;

  if (!Number.isSafeInteger(combined))
    console.warn(combined, 'exceeds MAX_SAFE_INTEGER. Precision may be lost');

  return combined;
}

Alternatively, if you need full 64-bit range, you can create a BigInt.

function getUint64BigInt(dataview, byteOffset, littleEndian) {
  // split 64-bit number into two 32-bit (4-byte) parts
  const left = dataview.getUint32(byteOffset, littleEndian);
  const right = dataview.getUint32(byteOffset + 4, littleEndian);

  // combine the two 32-bit values as their hex string representations
  const combined = littleEndian ?
    right.toString(16) + left.toString(16).padStart(8, '0') :
    left.toString(16) + right.toString(16).padStart(8, '0');

  return BigInt(`0x${combined}`);
}

属性

所有 DataView 实例都继承自 DataView.prototype,我们也可以向 DataView 对象中添加其他属性。

DataView.prototype.constructor
指定用来生成原型的构造函数.初始化值是标准内置DataView构造器.
DataView.prototype.buffer 只读
被视图引入的ArrayBuffer.创建实例的时候已固化因此是只读的.
DataView.prototype.byteLength 只读
ArrayBuffer中读取的字节长度. 创建实例的时候已固化因此是只读的.
DataView.prototype.byteOffset 只读
ArrayBuffer读取时的偏移字节长度. 创建实例的时候已固化因此是只读的.

方法

DataView.prototype.getInt8()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处获取一个8-bit数(一个字节).
DataView.prototype.getUint8()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处获取一个8-bit数(无符号字节).
DataView.prototype.getInt16()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处获取一个16-bit数(短整型).
DataView.prototype.getUint16()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处获取一个16-bit数(无符号短整型).
DataView.prototype.getInt32()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处获取一个32-bit数(长整型).
DataView.prototype.getUint32()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处获取一个32-bit数(无符号长整型).
DataView.prototype.getFloat32()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处获取一个32-bit数(浮点型).
DataView.prototype.getFloat64()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处获取一个64-bit数(双精度浮点型).

DataView.prototype.setInt8()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处储存一个8-bit数(一个字节).
DataView.prototype.setUint8()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处储存一个8-bit数(无符号字节).
DataView.prototype.setInt16()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处储存一个16-bit数(短整型).
DataView.prototype.setUint16()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处储存一个16-bit数(无符号短整型).
DataView.prototype.setInt32()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处储存一个32-bit数(长整型).
DataView.prototype.setUint32()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处储存一个32-bit数(无符号长整型).
DataView.prototype.setFloat32()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处储存一个32-bit数(浮点型).
DataView.prototype.setFloat64()
DataView起始位置以byte为计数的指定偏移量(byteOffset)处储存一个64-bit数(双精度浮点型).

示例

var buffer = new ArrayBuffer(16);
var dv = new DataView(buffer, 0);

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

规范

规范名称 状态 注释
Typed Array Specification Obsolete 已被 ECMAScript 6 取代
ECMAScript 2015 (6th Edition, ECMA-262)
DataView
Standard ECMA 标准中的初始版本
ECMAScript Latest Draft (ECMA-262)
DataView
Draft  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
DataViewChrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
DataView() without new throwsChrome Full support YesEdge Full support YesFirefox Full support 40IE No support NoOpera Full support YesSafari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android Full support 40Opera Android ? Safari iOS ? Samsung Internet Android ? nodejs Full support 0.12
SharedArrayBuffer accepted as bufferChrome Full support 60Edge ? Firefox Full support 55IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android Full support 55Opera Android ? Safari iOS ? Samsung Internet Android ? nodejs ?
bufferChrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
byteLengthChrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
byteOffsetChrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
getFloat32Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
getFloat64Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
getInt16Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
getInt32Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
getInt8Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
getUint16Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
getUint32Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
getUint8Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
setFloat32Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
setFloat64Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
setInt16Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
setInt32Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
setInt8Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
setUint16Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
setUint32Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
setUint8Chrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
prototypeChrome Full support 9Edge Full support 12Firefox Full support 15IE Full support 10Opera Full support 12.1Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

兼容性提示(英文原文中已去除)

从 FireFox 40 开始, DataView 对象需要通过 new 操作符来构造。如果不使用 new 而是直接将 DataView() 作为函数调用的话会抛出 TypeError 异常。

var dv = DataView(buffer, 0); 
// 抛出异常,错误信息为:
// TypeError: calling a builtin DataView constructor without new is forbidden
var dv = new DataView(buffer, 0);

相关链接

  • jDataView:DataView 的垫片(polyfill)库,使其能够用于所有浏览器及 Node.js 环境。同时,该库还对 DataView 进行了一些扩展。
  • ArrayBuffer
  • SharedArrayBuffer

文档标签和贡献者

最后编辑者: RainSlide,