此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Atomics.exchange()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2021年12月⁩.

Atomics.exchange() 静态方法会将给定的值替换数组上指定位置的值,并返回该位置的旧值。此原子操作保证在修改后的值写回之前不会发生其他写操作。

尝试一下

// 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
Atomics.exchange(typedArray, index, value)

参数

typedArray

一个整数类型数组。Int8ArrayUint8ArrayInt16ArrayUint16ArrayInt32ArrayUint32ArrayBigInt64ArrayBigUint64Array 之一。

index

typedArray 中的要替换为 value 的位置。

value

要替换的数字。

返回值

给定位置的旧值(typedArray[index])。

异常

TypeError

如果 typedArray 不是允许的整数类型数组之一,则抛出该异常。

RangeError

如果 index 超出 typedArray 的范围,则抛出该异常。

示例

使用 exchange()

js
const sab = new SharedArrayBuffer(1024);
const ta = new Uint8Array(sab);

Atomics.exchange(ta, 0, 12); // 返回 0,即旧的值
Atomics.load(ta, 0); // 12

规范

Specification
ECMAScript® 2026 Language Specification
# sec-atomics.exchange

浏览器兼容性

参见