JavaScript DataView getBigInt64() 方法用于检索从此 DataView 的指定字节偏移量开始的 8 字节整数值。它将这些字节解码为 64 位有符号整数。此外,您可以从 DataView 边界内的任何偏移量中检索多个字节。
如果 byteOffset 参数的值超出此 DataView 的边界,并且尝试使用 getBigInt64() 方法检索数据将引发 'RangeError' 异常。
语法
以下是 JavaScript DataView getBigInt64() 方法的语法 -
getBigInt64(byteOffset, littleEndian)
参数
此方法接受两个名为 'byteOffset' 和 'littleEndian' 的参数,如下所述 -
- byteOffset - DataView 中要从中读取数据的位置。
- littleEndian − 它表示数据是以 little-endian 还是 big endian 格式存储。
返回值
此方法返回范围从 -263 到 263-1 的 BigInt(包括在内)。
示例 1
以下是 JavaScript DataView getBigInt64() 方法的基本示例。
<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);
//storing the value
data_view.setBigInt64(byteOffset, value);
//using the getBigInt64() method
document.write("<br>The store value: ", data_view.getBigInt64(byteOffset));
</script>
</body>
</html>
输出
上述程序返回存储的值。
The byte offset: 0
Value: 9223372036854775807
The store value: 9223372036854775807
Value: 9223372036854775807
The store value: 9223372036854775807
示例 2
如果 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);
//storing the value
data_view.setBigInt64(byteOffset, value);
try {
//using the getBigInt64() method
document.write("<br>The store value: ", data_view.getBigInt64(-1));
} catch (error) {
document.write("<br>", error);
}
</script>
</body>
</html>
输出
执行完上面的程序后,会抛出一个异常 −
The byte offset: 1
Value: 9223372036854775807
RangeError: Offset is outside the bounds of the DataView
Value: 9223372036854775807
RangeError: Offset is outside the bounds of the DataView