JavaScript DataView setBigInt64() 方法
JavaScript DataView 的 setBigInt64() 方法接受一个大整数,并将其作为 64 位有符号整数存储在此数据视图中从指定字节偏移量开始的 8 字节段中。此外,您还可以在边界内的任意偏移量处存储多个字节值。
如果 byteOffset 参数的值超出此边界,则此方法会抛出 'RangeError' 异常。如果给定的值不符合 bigInt 有符号整数的格式,则会抛出 'TypeError' 异常。
语法
以下是 JavaScript DataView setBigInt64() 方法的语法 -
setBigInt64(byteOffset, value, littleEndian)
参数
此方法接受三个参数:'byteOffset'、'value' 和 'littleEndian',具体描述如下 -
- byteOffset - DataView 中存储字节的位置。
- 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;
//找到可能的最大 BigInt 值
const value = 2n ** (64n - 1n) - 1n;
document.write("The byte offset: ", byteOffset);
document.write("<br>Value: ", value);
//使用 setBigInt64() 方法
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;
//找到可能的最大 BigInt 值
const value = 2n ** (64n - 1n) - 1n;
document.write("The byte offset: ", byteOffset);
document.write("<br>Value: ", value);
//使用 setBigInt64() 方法
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;
//找到可能的最大 BigInt 值
const value = 2n ** (64n - 1n) - 1n;
document.write("The byte offset: ", byteOffset);
document.write("<br>Value: ", value);
try {
//使用 setBigInt64() 方法
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;
//找到可能的最大 BigInt 值
const value = 2345678765432;
document.write("The byte offset: ", byteOffset);
document.write("<br>Value: ", value);
try {
//使用 setBigInt64() 方法
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

