JavaScript DataView setBigInt64() 方法



JavaScript DataView 的 setBigInt64() 方法接受一個大整數,並將其作為64 位有符號整數儲存在這個資料檢視中指定位元組偏移量開始的 8 位元組段中。此外,您可以在邊界內的任何偏移量處儲存多個位元組值。

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

語法

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

setBigInt64(byteOffset, value, littleEndian)

引數

此方法接受三個引數“byteOffset”、“value”和“littleEndian”,如下所述:

  • byteOffset - 資料檢視中將儲存位元組的位置。
  • value - 需要儲存的 64 位有符號整數。
  • littleEndian - 指示資料是按小端序還是大端序儲存。

返回值

此方法返回'undefined'

示例 1

以下程式演示了 JavaScript DataView setBigInt64() 方法的用法。

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 0;
   //find the highest possible BigInt value 
   const value = 2n ** (64n - 1n) - 1n;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>Value: ", value);
   //using the setBigInt64() method
   data_view.setBigInt64(byteOffset, value);
   document.write("<br>The stored value is: ", data_view.getBigInt64(byteOffset)); 
</script>
</body>
</html>

輸出

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

The byte offset: 0
Value: 9223372036854775807
The stored value is: 9223372036854775807

示例 2

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

以下是 JavaScript DataView setBigInt64() 方法的另一個示例。我們使用此方法將 BigInt 值儲存為64 位有符號整數,從資料檢視中指定的 byteOffset 值1開始。

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

輸出

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

The byte offset: 1
Value: 9223372036854775807
The data_view.setBigInt64() method returns: undefined

示例 3

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

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = -1;
   //find the highest possible BigInt value 
   const value = 2n ** (64n - 1n) - 1n;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>Value: ", value);
   try {
      //using the setBigInt64() method
      data_view.setBigInt64(byteOffset, value);
   } catch (error) {
      document.write("<br>", error);
   } 
</script>
</body>
</html>

輸出

執行上述程式後,它將丟擲 'RangeError' 異常,如下所示:

The byte offset: -1
Value: 9223372036854775807
RangeError: Offset is outside the bounds of the DataView

示例 4

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

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 1;
   //find the highest possible BigInt value 
   const value = 2345678765432;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>Value: ", value);
   try {
      //using the setBigInt64() method
      data_view.setBigInt64(byteOffset, value);
   } catch (error) {
      document.write("<br>", error);
   } 
</script>
</body>
</html>

輸出

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

The byte offset: 1
Value: 2345678765432
TypeError: Cannot convert 2345678765432 to a BigInt
廣告