JavaScript DataView getBigInt64() 方法
JavaScript DataView 的 getBigInt64() 方法用于检索从此 DataView 指定字节偏移量开始的 8 字节整数值。它将这些字节解码为 64 位有符号整数。此外,您还可以从 DataView 边界内的任意偏移量检索多个字节。
如果 byteOffset 参数的值超出此 DataView 的边界,则尝试使用 getBigInt64() 方法检索数据将引发 'RangeError' 异常。
语法
以下是 JavaScript DataView getBigInt64() 方法的语法 -
getBigInt64(byteOffset, littleEndian)
参数
此方法接受两个名为"byteOffset"和"littleEndian"的参数,如下所述 -
- byteOffset - DataView 中读取数据的位置。
- littleEndian − 指示数据是以小端还是大端格式存储的。
返回值
此方法返回一个 BigInt,范围从 -263 到 263-1(含)。
示例 1
以下是 JavaScript DataView getBigInt64() 方法的基本示例。
<html>
<body>
<script>
const buffer = new ArrayBuffer(16);
const data_view = new DataView(buffer);
const byteOffset = 0;
//找到可能的最大 BigInt 值
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);
//使用 getBigInt64() 方法
document.write("<br>The store value: ", data_view.getBigInt64(byteOffset));
</script>
</body>
</html>
输出
上述程序返回存储的值。
The byte offset: 0 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;
//找到可能的最大 BigInt 值
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 {
//使用 getBigInt64() 方法
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

