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 December 2021.
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
-
정수 타입의 배열.
Int8Array
,Uint8Array
,Int16Array
,Uint16Array
,Int32Array
,Uint32Array
,BigInt64Array
,BigUint64Array
중 하나. index
-
value
를 교환할typedArray
의 위치. value
-
교환할 숫자.
반환 값
해당 위치의 예전 값(typedArray[index]
).
예외
typedArray
가 허용하는 정수 타입이 아닐 경우TypeError
가 발생합니다.index
가 해당typedArray
를 벗어나는 경우RangeError
가 발생합니다.
예제
exchange() 사용하기
js
const sab = new SharedArrayBuffer(1024);
const ta = new Uint8Array(sab);
Atomics.exchange(ta, 0, 12); // returns 0, the old value
Atomics.load(ta, 0); // 12
명세서
Specification |
---|
ECMAScript® 2025 Language Specification # sec-atomics.exchange |
브라우저 호환성
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
exchange |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.