Estás leyendo la versión en inglés del artículo porque aún no existe una traducción para este idioma. ¡Ayúdanos a traducir este artículo!
DataView provee una interfaz de bajo nivel para la lectura y escritura de diferentes tipos de números dentro de un ArrayBuffer sin importar el endianness de la plataforma.
El código fuente de este ejemplo interactivo se encuentra almacenado en un repositorio en Github. Si te interesa contribuir al proyecto de ejemplos interactivos, por favor clona https://github.com/mdn/interactive-examples y envíanos un pull request.
Sintaxis
new DataView(buffer [, byteOffset [, byteLength]])
Parámetros
buffer- Un
ArrayBufferoSharedArrayBufferexistente que será utilizado por el nuevo objetoDataView. byteOffsetOptional- El desplazamiento, en bytes, desde el primer byte en el buffer, que servirá como referencia para iniciar la nueva vista. Si no es especificado, la vista comenzará con el primer byte del buffer.
byteLengthOptional- El número de elementos en el array de bytes. Si no es especificado, el tamaño de la vista será el mismo tamaño del buffer.
Valor retornado
Un nuevo objeto DataView representando el buffer de datos especificado.
Excepciones
RangeError- Esta excepción es lanzada si el
byteOffsety elbyteLengthresultante en la nueva vista sobrepasan el final del buffer.
Description
Endianness
Multi-byte number formats are represented in memory differently depending on machine architecture, see Endianness for an explanation. DataView accessors provide explicit control of how data will be accessed irrespective of the platform architecture's endianness.
var littleEndian = (function() {
var buffer = new ArrayBuffer(2);
new DataView(buffer).setInt16(0, 256, true /* littleEndian */);
// Int16Array uses the platform's endianness.
return new Int16Array(buffer)[0] === 256;
})();
console.log(littleEndian); // true or false
Properties
All DataView instances inherit from DataView.prototype and allows the addition of properties to all DataView objects.
DataView.prototype.constructor- Specifies the function that creates an object's prototype. The initial value is the standard built-in
DataViewconstructor. DataView.prototype.bufferRead only- The
ArrayBufferreferenced by this view. Fixed at construction time and thus read only. DataView.prototype.byteLengthRead only- The length (in bytes) of this view from the start of its
ArrayBuffer. Fixed at construction time and thus read only. DataView.prototype.byteOffsetRead only- The offset (in bytes) of this view from the start of its
ArrayBuffer. Fixed at construction time and thus read only.
Methods
Read
DataView.prototype.getInt8()- Gets a signed 8-bit integer (byte) at the specified byte offset from the start of the view.
DataView.prototype.getUint8()- Gets an unsigned 8-bit integer (unsigned byte) at the specified byte offset from the start of the view.
DataView.prototype.getInt16()- Gets a signed 16-bit integer (short) at the specified byte offset from the start of the view.
DataView.prototype.getUint16()- Gets an unsigned 16-bit integer (unsigned short) at the specified byte offset from the start of the view.
DataView.prototype.getInt32()- Gets a signed 32-bit integer (long) at the specified byte offset from the start of the view.
DataView.prototype.getUint32()- Gets an unsigned 32-bit integer (unsigned long) at the specified byte offset from the start of the view.
DataView.prototype.getFloat32()- Gets a signed 32-bit float (float) at the specified byte offset from the start of the view.
DataView.prototype.getFloat64()- Gets a signed 64-bit float (double) at the specified byte offset from the start of the view.
DataView.prototype.getBigInt64()- Gets a signed 64-bit integer (long long) at the specified byte offset from the start of the view.
DataView.prototype.getBigUint64()- Gets an unsigned 64-bit integer (unsigned long long) at the specified byte offset from the start of the view.
Write
DataView.prototype.setInt8()- Stores a signed 8-bit integer (byte) value at the specified byte offset from the start of the view.
DataView.prototype.setUint8()- Stores an unsigned 8-bit integer (unsigned byte) value at the specified byte offset from the start of the view.
DataView.prototype.setInt16()- Stores a signed 16-bit integer (short) value at the specified byte offset from the start of the view.
DataView.prototype.setUint16()- Stores an unsigned 16-bit integer (unsigned short) value at the specified byte offset from the start of the view.
DataView.prototype.setInt32()- Stores a signed 32-bit integer (long) value at the specified byte offset from the start of the view.
DataView.prototype.setUint32()- Stores an unsigned 32-bit integer (unsigned long) value at the specified byte offset from the start of the view.
DataView.prototype.setFloat32()- Stores a signed 32-bit float (float) value at the specified byte offset from the start of the view.
DataView.prototype.setFloat64()- Stores a signed 64-bit float (double) value at the specified byte offset from the start of the view.
DataView.prototype.setBigInt64()- Stores a signed 64-bit integer (long long) value at the specified byte offset from the start of the view.
DataView.prototype.setBigUint64()- Stores an unsigned 64-bit integer (unsigned long long) value at the specified byte offset from the start of the view.
Example
var buffer = new ArrayBuffer(16); var dv = new DataView(buffer, 0); dv.setInt16(1, 42); dv.getInt16(1); //42
Specifications
| Specification | Status | Comment |
|---|---|---|
| Typed Array Specification | Obsolete | Superseded by ECMAScript 6 |
| ECMAScript 2015 (6th Edition, ECMA-262) La definición de 'DataView' en esta especificación. |
Standard | Initial definition in an ECMA standard |
| ECMAScript Latest Draft (ECMA-262) La definición de 'DataView' en esta especificación. |
Draft |
Browser compatibility
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DataView | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
buffer | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
byteLength | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
byteOffset | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
getBigInt64 | Chrome Soporte completo 67 | Edge Sin soporte No | Firefox Soporte completo 68 | IE Sin soporte No | Opera Soporte completo 54 | Safari Sin soporte No | WebView Android Soporte completo 67 | Chrome Android Soporte completo 67 | Firefox Android Soporte completo 68 | Opera Android Soporte completo 48 | Safari iOS Sin soporte No | Samsung Internet Android Soporte completo 9.0 | nodejs Soporte completo 10.4.0 |
getBigUint64 | Chrome Soporte completo 67 | Edge Sin soporte No | Firefox Soporte completo 68 | IE Sin soporte No | Opera Soporte completo 54 | Safari Sin soporte No | WebView Android Soporte completo 67 | Chrome Android Soporte completo 67 | Firefox Android Soporte completo 68 | Opera Android Soporte completo 48 | Safari iOS Sin soporte No | Samsung Internet Android Soporte completo 9.0 | nodejs Soporte completo 10.4.0 |
getFloat32 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
getFloat64 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
getInt16 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
getInt32 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
getInt8 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
getUint16 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
getUint32 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
getUint8 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
DataView() without new throws | Chrome Soporte completo 11 | Edge Soporte completo 13 | Firefox Soporte completo 40 | IE Sin soporte No | Opera Soporte completo Si | Safari ? | WebView Android Soporte completo ≤37 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 40 | Opera Android Soporte completo Si | Safari iOS ? | Samsung Internet Android Soporte completo 1.0 | nodejs Soporte completo 0.12 |
prototype | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
setBigInt64 | Chrome Soporte completo 67 | Edge Sin soporte No | Firefox Soporte completo 68 | IE Sin soporte No | Opera Soporte completo 54 | Safari Sin soporte No | WebView Android Soporte completo 67 | Chrome Android Soporte completo 67 | Firefox Android Soporte completo 68 | Opera Android Soporte completo 48 | Safari iOS Sin soporte No | Samsung Internet Android Soporte completo 9.0 | nodejs Soporte completo 10.4.0 |
setBigUint64 | Chrome Soporte completo 67 | Edge Sin soporte No | Firefox Soporte completo 68 | IE Sin soporte No | Opera Soporte completo 54 | Safari Sin soporte No | WebView Android Soporte completo 67 | Chrome Android Soporte completo 67 | Firefox Android Soporte completo 68 | Opera Android Soporte completo 48 | Safari iOS Sin soporte No | Samsung Internet Android Soporte completo 9.0 | nodejs Soporte completo 10.4.0 |
setFloat32 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
setFloat64 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
setInt16 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
setInt32 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
setInt8 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
setUint16 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
setUint32 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
setUint8 | Chrome Soporte completo 9 | Edge Soporte completo 12 | Firefox Soporte completo 15 | IE Soporte completo 10 | Opera Soporte completo 12.1 | Safari Soporte completo 5.1 | WebView Android Soporte completo 4 | Chrome Android Soporte completo 18 | Firefox Android Soporte completo 15 | Opera Android Soporte completo 12.1 | Safari iOS Soporte completo 4.2 | Samsung Internet Android Soporte completo Si | nodejs Soporte completo Si |
SharedArrayBuffer accepted as buffer | Chrome Soporte completo 60 | Edge Sin soporte No | Firefox Soporte completo 55 | IE Sin soporte No | Opera Soporte completo 47 | Safari ? | WebView Android Soporte completo 60 | Chrome Android Soporte completo 60 | Firefox Android Soporte completo 55 | Opera Android ? | Safari iOS ? | Samsung Internet Android Soporte completo 8.0 | nodejs ? |
Leyenda
- Soporte completo
- Soporte completo
- Sin soporte
- Sin soporte
- Compatibilidad desconocida
- Compatibilidad desconocida
Compatibility notes
Starting with Firefox 40, DataView requires to be constructed with a new operator. Calling DataView() as a function without new, will throw a TypeError from now on.
var dv = DataView(buffer, 0); // TypeError: calling a builtin DataView constructor without new is forbidden
var dv = new DataView(buffer, 0);
See also
- jDataView: JavaScript library that polyfills and extends the
DataViewAPI to all browsers and Node.js. ArrayBufferSharedArrayBuffer