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
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.
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. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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
|
Edge 79 |
Firefox
79
|
IE No | Opera No | Safari 10.1 — 11 |
WebView Android
60 — 63
|
Chrome Android
60 — 63
|
Firefox Android
57
Disabled
|
Opera Android No | Safari iOS 10.3 — 11 |
Samsung Internet Android
No
|
nodejs 8.10.0 |
完整支持
不支持
见实现注意事项。
用户必须明确启用此特征。
DataView
DataView.prototype.getBigInt64()
DataView.prototype.getBigUint64()
DataView.prototype.getFloat32()
DataView.prototype.getFloat64()
DataView.prototype.getInt16()
DataView.prototype.getInt32()
DataView.prototype.getInt8()
DataView.prototype.getUint16()
DataView.prototype.getUint32()
DataView.prototype.getUint8()
DataView.prototype.setBigInt64()
DataView.prototype.setBigUint64()
DataView.prototype.setFloat32()
DataView.prototype.setFloat64()
DataView.prototype.setInt16()
DataView.prototype.setInt32()
DataView.prototype.setInt8()
DataView.prototype.setUint16()
DataView.prototype.setUint32()
DataView.prototype.setUint8()
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()