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

View in English Always switch to English

Atomics.load()

基线 广泛可用

自 2021年12月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

Atomics.load() 静态方法返回数组中指定位置的值。

尝试一下

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

参数

typedArray

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

index

typedArray 中的要加载的位置。

返回值

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

异常

TypeError

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

RangeError

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

示例

使用 load()

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

Atomics.add(ta, 0, 12);
Atomics.load(ta, 0); // 12

规范

规范
ECMAScript® 2027 Language Specification
# sec-atomics.load

浏览器兼容性

参见