DataView() constructor is used to create DataView 对象。

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

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

参数

buffer
An existing ArrayBuffer or SharedArrayBuffer to use as the storage backing the new DataView 对象。
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.

返回值

A new DataView object representing the specified data buffer. (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

Thrown if the byteOffset or byteLength parameter values result in the view extending past the end of the 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.

范例

Using DataView

var buffer = new ArrayBuffer(16);
var view = new DataView(buffer, 0);
view.setInt16(1, 42);
view.getInt16(1); // 42
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'DataView constructor' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
DataView() 构造函数 Chrome 9 Edge 12 Firefox 15 IE 10 Opera 12.1 Safari 5.1 WebView Android 4 Chrome Android 18 Firefox Android 15 Opera Android 12.1 Safari iOS 4.2 Samsung Internet Android 1.0 nodejs 0.10
DataView() without new throws Chrome 11 Edge 13 Firefox 40 IE No Opera 15 Safari 5.1 WebView Android ≤37 Chrome Android 18 Firefox Android 40 Opera Android 14 Safari iOS 5.1 Samsung Internet Android 1.0 nodejs 0.10
SharedArrayBuffer accepted as buffer Chrome 68
68
不支持 60 — 63
Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks . This was a temporary removal while mitigations were put in place.
Edge 79 Firefox 79
79
57 Disabled
Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog) .
Disabled From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 55 — 57
不支持 46 — 55 Disabled
Disabled ). To change preferences in Firefox, visit
IE No Opera No Safari 10.1 — 11 WebView Android 60 — 63
不支持 60 — 63
Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks . This is intended as a temporary measure until other mitigations are in place.
Chrome Android 60 — 63
不支持 60 — 63
Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks . This is intended as a temporary measure until other mitigations are in place.
Firefox Android 57 Disabled
57 Disabled
Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog) .
Disabled From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 55 — 57
不支持 46 — 55 Disabled
Disabled ). To change preferences in Firefox, visit
Opera Android No Safari iOS 10.3 — 11 Samsung Internet Android No
不支持 No
Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks . This is intended as a temporary measure until other mitigations are in place.
nodejs 8.10.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改:
  1. 标准内置对象
  2. DataView
  3. 特性
    1. DataView.prototype.buffer
    2. DataView.prototype.byteLength
    3. DataView.prototype.byteOffset
  4. 方法
    1. DataView.prototype.getBigInt64()
    2. DataView.prototype.getBigUint64()
    3. DataView.prototype.getFloat32()
    4. DataView.prototype.getFloat64()
    5. DataView.prototype.getInt16()
    6. DataView.prototype.getInt32()
    7. DataView.prototype.getInt8()
    8. DataView.prototype.getUint16()
    9. DataView.prototype.getUint32()
    10. DataView.prototype.getUint8()
    11. DataView.prototype.setBigInt64()
    12. DataView.prototype.setBigUint64()
    13. DataView.prototype.setFloat32()
    14. DataView.prototype.setFloat64()
    15. DataView.prototype.setInt16()
    16. DataView.prototype.setInt32()
    17. DataView.prototype.setInt8()
    18. DataView.prototype.setUint16()
    19. DataView.prototype.setUint32()
    20. DataView.prototype.setUint8()
  5. 继承:
  6. Function
  7. 特性
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  8. 方法
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()
  9. Object
  10. 特性
    1. Object.prototype.__proto__
    2. Object.prototype.constructor
  11. 方法
    1. Object.prototype.__defineGetter__()
    2. Object.prototype.__defineSetter__()
    3. Object.prototype.__lookupGetter__()
    4. Object.prototype.__lookupSetter__()
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.prototype.toLocaleString()
    9. Object.prototype.toSource()
    10. Object.prototype.toString()
    11. Object.prototype.valueOf()
    12. Object.setPrototypeOf()

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1