在线文档教程

Atomics.exchange

Atomics.exchange

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

语法

Atomics.exchange(typedArray, index, value)

参数

typedArray一个共享的整型 typed array。例如 Int8ArrayUint8ArrayInt16ArrayUint16ArrayInt32Array,或Uint32Arrayindex被替换的 typedArray 值的索引。value去替换的值。

返回值

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

异常

  • 假如 typedArray 不是允许的整型之一,则抛出 TypeError

  • 假如 typedArray 不是一个贡献的 typed array,则抛出 TypeError

  • 如果 index 超出了 typedArray 的边界,则抛出 RangeError

示例

var sab = new SharedArrayBuffer(1024 var ta = new Uint8Array(sab Atomics.exchange(ta, 0, 12 // returns 0, the old value Atomics.load(ta, 0 // 12

规范

SpecificationStatusComment
ECMAScript Latest Draft (ECMA-262)The definition of 'Atomics.exchange' in that specification.Living StandardInitial definition in ES2017.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic supportNo support 2No support55 (55) 1No supportNo supportNo support

FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic supportNo supportNo support55.0 (55) 1No supportNo supportNo support