JavaScript的Atomic对象是一个对象,它提供原子操作,例如add,sub和或xor,load,store等作为静态方法,这些方法与SharedArrayBuffer对象一起使用。
Atomic对象的load()函数返回数组给定位置的值。
其语法如下
Atomics.load()
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var arrayBuffer = new SharedArrayBuffer(16); var data = new Uint8Array(arrayBuffer); data[0] = 20; Atomics.add(data, 0, 30); document.write(Atomics.load(data, 0)); </script> </body> </html>
输出结果
50
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var arrayBuffer = new SharedArrayBuffer(16); var data = new Uint8Array(arrayBuffer); data[0] = 3; document.write(Atomics.add(data, 0, 3)); document.write(", "+Atomics.load(data, 0)); </script> </body> </html>
输出结果
3, 6