Atomics.exchange() 静态方法会将给定的值替换数组上指定位置的值,并返回该位置的旧值。此原子操作保证在修改后的值写回之前不会发生其他写操作。
{{InteractiveExample("JavaScript Demo: Atomics.exchange()")}}
```js interactive-example // Create a SharedArrayBuffer with a size in bytes const buffer = new SharedArrayBuffer(16); const uint8 = new Uint8Array(buffer); uint8[0] = 5;
console.log(Atomics.load(uint8, 0)); // Expected output: 5
Atomics.exchange(uint8, 0, 2); // Returns 5 console.log(Atomics.load(uint8, 0)); // Expected output: 2
## 语法
```js-nolint
Atomics.exchange(typedArray, index, value)
参数
typedArray- : 一个整数类型数组。
Int8Array、Uint8Array、Int16Array、Uint16Array、Int32Array、Uint32Array、BigInt64Array或BigUint64Array之一。
- : 一个整数类型数组。
index- :
typedArray中的要替换为value的位置。
- :
value- : 要替换的数字。
返回值
给定位置的旧值(typedArray[index])。
异常
TypeError- : 如果
typedArray不是允许的整数类型数组之一,则抛出该异常。
- : 如果
RangeError- : 如果
index超出typedArray的范围,则抛出该异常。
- : 如果
示例
使用 exchange()
const sab = new SharedArrayBuffer(1024);
const ta = new Uint8Array(sab);
Atomics.exchange(ta, 0, 12); // 返回 0,即旧的值
Atomics.load(ta, 0); // 12