Atomics.sub()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since December 2021.
Die statische Methode Atomics.sub()
subtrahiert einen gegebenen Wert an einer bestimmten Position im Array und gibt den alten Wert an dieser Position zurück. Diese atomare Operation garantiert, dass keine andere Schreibvorgänge stattfinden, bis der modifizierte Wert zurückgeschrieben wird.
Probieren Sie es aus
// Create a SharedArrayBuffer with a size in bytes
const buffer = new SharedArrayBuffer(16);
const uint8 = new Uint8Array(buffer);
uint8[0] = 7;
// 7 - 2 = 5
console.log(Atomics.sub(uint8, 0, 2));
// Expected output: 7
console.log(Atomics.load(uint8, 0));
// Expected output: 5
Syntax
Atomics.sub(typedArray, index, value)
Parameter
typedArray
-
Ein Integer-Typ-Array. Eine der folgenden:
Int8Array
,Uint8Array
,Int16Array
,Uint16Array
,Int32Array
,Uint32Array
,BigInt64Array
, oderBigUint64Array
. index
-
Die Position in
typedArray
, von der einvalue
subtrahiert wird. value
-
Die Zahl, die subtrahiert werden soll.
Rückgabewert
Der alte Wert an der gegebenen Position
(typedArray[index]
).
Ausnahmen
TypeError
-
Wird ausgelöst, wenn
typedArray
nicht einer der erlaubten Integer-Typen ist. RangeError
-
Wird ausgelöst, wenn
index
außerhalb der Grenzen vontypedArray
liegt.
Beispiele
Verwendung von sub
const sab = new SharedArrayBuffer(1024);
const ta = new Uint8Array(sab);
ta[0] = 48;
Atomics.sub(ta, 0, 12); // returns 48, the old value
Atomics.load(ta, 0); // 36
Spezifikationen
Specification |
---|
ECMAScript® 2026 Language Specification # sec-atomics.sub |