[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/DataView [Back]  [Original]

DataView - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

DataView

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

* Some parts of this feature may have varying levels of support.

DataView - ArrayBuffer, .

In this article

- ( Endianness ). DataView .

js
const littleEndian = (() => {
  const buffer = new ArrayBuffer(2);
  new DataView(buffer).setInt16(0, 256, true /* littleEndian */);

  // Int16Array    .
  return new Int16Array(buffer)[0] === 256;
})();

console.log(littleEndian); // true  false

64-

DataView.prototype.setBigInt64() DataView.prototype.setBigUint64(). 64- , , getUint64() Number.MAX_SAFE_INTEGER, .

js
function getUint64(dataview, byteOffset, littleEndian) {
  //  64-    32- ( 4 ) 
  const left = dataview.getUint32(byteOffset, littleEndian);
  const right = dataview.getUint32(byteOffset + 4, littleEndian);

  //   32- 
  const combined = littleEndian
    ? left + 2 ** 32 * right
    : 2 ** 32 * left + right;

  if (!Number.isSafeInteger(combined))
    console.warn(
      combined,
      " MAX_SAFE_INTEGER.    ",
    );

  return combined;
}

64- , BigInt. BigInt , , BigInt , 32- , - JavaScript.

js
const BigInt = window.BigInt,
  bigThirtyTwo = BigInt(32),
  bigZero = BigInt(0);
function getUint64BigInt(dataview, byteOffset, littleEndian) {
  //  64-    32- ( 4 ) 
  const left = BigInt(dataview.getUint32(byteOffset | 0, !!littleEndian) >>> 0);
  const right = BigInt(
    dataview.getUint32(((byteOffset | 0) + 4) | 0, !!littleEndian) >>> 0,
  );

  //   32-   
  return littleEndian
    ? (right << bigThirtyTwo) | left
    : (left << bigThirtyTwo) | right;
}

DataView()

DataView.

DataView.prototype DataView.

DataView.prototype.buffer

ArrayBuffer, . .

DataView.prototype.byteLength

. .

DataView.prototype.byteOffset

ArrayBuffer. .

DataView.prototype.constructor

-, . DataView DataView.

DataView.prototype[@@toStringTag]

@@toStringTag "DataView". Object.prototype.toString().

DataView.prototype.getBigInt64()

8 DataView 64- .

DataView.prototype.getBigUint64()

8 DataView 64- .

DataView.prototype.getFloat32()

4 DataView 32- .

DataView.prototype.getFloat64()

8 DataView 64- .

DataView.prototype.getInt16()

2 DataView 16- .

DataView.prototype.getInt32()

4 DataView 32- .

DataView.prototype.getInt8()

1 DataView 8- .

DataView.prototype.getUint16()

2 DataView 16- .

DataView.prototype.getUint32()

4 DataView 32- .

DataView.prototype.getUint8()

1 DataView 8- .

DataView.prototype.setBigInt64()

BigInt 64- 8 DataView.

DataView.prototype.setBigUint64()

BigInt 64- 8 DataView.

DataView.prototype.setFloat32()

32- 4 DataView.

DataView.prototype.setFloat64()

64- 8 DataView.

DataView.prototype.setInt16()

16- 2 DataView.

DataView.prototype.setInt32()

32- 4 DataView.

DataView.prototype.setInt8()

8- DataView.

DataView.prototype.setUint16()

16- 2 DataView.

DataView.prototype.setUint32()

32- 4 DataView.

DataView.prototype.setUint8()

8- DataView.

DataView

js
const buffer = new ArrayBuffer(16);
const view = new DataView(buffer, 0);

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

Specification
ECMAScript 2027 LanguageSpecification
# sec-dataview-objects


Web Proxy Viewer  |  New URL  |  Original Page