JavaScript DataView setBigInt64() 方法接受一个大整数,并将其作为 64 位有符号整数存储在 8 字节段中,从此数据视图中的指定字节偏移量开始。此外,您可以在边界内的任何偏移量处存储多个字节值。
如果 byteOffset 参数的值超出此范围,则此方法将引发 'RangeError' 异常。如果给定的值不适合 bigInt 有符号整数,它将引发 'TypeError' 异常。
语法
以下是 JavaScript DataView setBigInt64() 方法的语法 -
setBigInt64(byteOffset, value, littleEndian)
参数
此方法接受三个参数 'byteOffset'、'value' 和 'littleEndian',如下所述 -
- byteOffset - DataView 中将存储字节的位置。
- value - 需要存储的有符号 64 位整数。
- littleEndian − 它指示数据是以 little-endian 还是 big-endian 格式存储。
返回值
此方法返回 '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
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
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
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
Value: 2345678765432
TypeError: Cannot convert 2345678765432 to a BigInt