Atomics.load() 静态方法返回数组中指定位置的值。
{{InteractiveExample("JavaScript Demo: Atomics.load()")}}
```js interactive-example // Create a SharedArrayBuffer with a size in bytes const buffer = new SharedArrayBuffer(16); const uint8 = new Uint8Array(buffer); uint8[0] = 5;
// 5 + 2 = 7 console.log(Atomics.add(uint8, 0, 2)); // Expected output: 5
console.log(Atomics.load(uint8, 0)); // Expected output: 7
## 语法
```js-nolint
Atomics.load(typedArray, index)
参数
typedArray- : 一个整数类型数组。
Int8Array、Uint8Array、Int16Array、Uint16Array、Int32Array、Uint32Array、BigInt64Array或BigUint64Array之一。
- : 一个整数类型数组。
index- :
typedArray中的要加载的位置。
- :
返回值
给定位置的值(typedArray[index])。
异常
TypeError- : 如果
typedArray不是允许的整数类型数组之一,则抛出该异常。
- : 如果
RangeError- : 如果
index超出typedArray的范围,则抛出该异常。
- : 如果
示例
使用 load()
const sab = new SharedArrayBuffer(1024);
const ta = new Uint8Array(sab);
Atomics.add(ta, 0, 12);
Atomics.load(ta, 0); // 12