JavaScript DataView setBigUint64() 方法



JavaScript DataView 的 setBigUint64() 方法接收一個大整數,並將其儲存為從 DataView 中指定位元組偏移量開始的 8 位元組段中的 64 位無符號整數。

沒有嚴格的對齊要求;您可以在 DataView 範圍內的任何偏移量儲存多個位元組。

如果 byteOffset 引數的值超出此範圍,則此方法將丟擲 'RangeError' 異常,如果給定值不適合 bigInt 無符號整數,則會丟擲 'TypeError' 異常。

語法

以下是 JavaScript DataView setBigUint64() 方法的語法:

setBigUint64(byteOffset, value, littleEndian)

引數

此方法接受三個名為 'byteOffset'、'value' 和 'littleEndian' 的引數,如下所述:

  • byteOffset - DataView 中將儲存位元組的位置。
  • value - 需要儲存的 64 位無符號大整數。
  • littleEndian - 指示資料是以小端序還是大端序格式儲存。

返回值

此方法返回 'undefined',因為它僅儲存位元組值。

示例 1

以下是 JavaScript DataView setBigUint64() 方法的基本示例。

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 0;
   //find the highest possible BigInt value that fits in an unsigned 64-bit integer
   const value = 2n ** 64n - 1n;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>Value: ", value);
   //using the setBigUnit64() method
   data_view.setBigUint64(byteOffset, value);
   document.write("<br>The stored value: ", data_view.getBigUint64(byteOffset));
</script>
</body>
</html>

輸出

上述程式將指定的 bigInt 無符號值儲存在當前 DataView 中,並將其顯示為:

The byte offset: 0
Value: 18446744073709551615
The stored value: 18446744073709551615

示例 2

如果您嘗試列印此方法的結果,它將返回 'undefined' 作為輸出。

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 1;
   //find the highest possible BigInt value that fits in an unsigned 64-bit integer
   const value = 2n ** 64n - 1n;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>Value: ", value);
   //using the setBigUnit64() method
   document.write("<br>The data_view.setBigUnit64() method returns: ",    data_view.setBigUint64(byteOffset, value));
</script>
</body>
</html>

輸出

執行上述程式後,它將返回 'undefined' 結果。

The byte offset: 1
Value: 18446744073709551615
The data_view.setBigUnit64() method returns: undefined

示例 3

如果 byteOffset 引數的值超出此資料檢視的範圍,則會丟擲 'RangeError' 異常。

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 0;
   //find the highest possible BigInt value that fits in an unsigned 64-bit integer
   const value = 23456543212;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>Value: ", value);
   try {
      //using the setBigUnit64() method
      data_view.setBigUint64(byteOffset, value); 
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

輸出

執行上述程式後,它將丟擲 'RangeError' 異常。

The byte offset: 0
Value: 23456543212
TypeError: Cannot convert 23456543212 to a BigInt

示例 4

如果給定值不適合 bigInt 無符號整數,則此方法將丟擲 'TypeError' 異常。

<html>
<head>
<title>JavaScript DataView setBigUint64() Method</title>
</head>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 0;
   //find the highest possible BigInt value that fits in an unsigned 64-bit integer
   const value = 23456543212;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>Value: ", value);
   try {
      //using the setBigUnit64() method
      data_view.setBigUint64(byteOffset, value); 
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

輸出

上述程式丟擲 'TypeError' 異常,如下所示:

The byte offset: 0
Value: 23456543212
TypeError: Cannot convert 23456543212 to a BigInt
廣告