Atomics.load()
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-
一個整數型別化陣列。可以是
Int8Array、Uint8Array、Int16Array、Uint16Array、Int32Array、Uint32Array、BigInt64Array或BigUint64Array中的一種。 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® 2026 語言規範 # sec-atomics.load |
瀏覽器相容性
載入中…