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

语法

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

参数

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

返回值

一个由 buffer 生成的 DataView 对象。

异常

RangeError
如果由偏移(byteOffset)和字节长度(byteLength)计算得到的结束位置超出了 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 or false

属性

所有 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 AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome 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);

相关链接

文档标签和贡献者

最后编辑者: Jiang-Xuan,