aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/atomics/exchange/index.html
blob: 4502bf8a52076b16650756b06f0f1e5850689d11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
title: Atomics.exchange()
slug: Web/JavaScript/Reference/Global_Objects/Atomics/exchange
tags:
  - Atmoics
  - Experimental
  - JavaScript
  - Method
  - 共享内存
  - 实验性
translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/exchange
---
<div>{{JSRef}} {{SeeCompatTable}}</div>

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

<div>{{EmbedInteractiveExample("pages/js/atomics-exchange.html")}}</div>



<h2 id="语法">语法</h2>

<pre class="syntaxbox">Atomics.exchange(typedArray, index, value)
</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>typedArray</code></dt>
 <dd>一个共享的整型 typed array。例如 {{jsxref("Int8Array")}}{{jsxref("Uint8Array")}}{{jsxref("Int16Array")}}{{jsxref("Uint16Array")}}{{jsxref("Int32Array")}},或 {{jsxref("Uint32Array")}}</dd>
 <dt><code>index</code></dt>
 <dd>被替换的 <code>typedArray 值的索引。</code></dd>
 <dt><code>value</code></dt>
 <dd>去替换的值。</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>给定位置的旧值(<code>typedArray[index]</code>).</p>

<h3 id="错误">错误</h3>

<ul>
 <li>假如 <code>typedArray</code> 不是允许的整型之一,则抛出 {{jsxref("TypeError")}}</li>
 <li><code><font face="Open Sans, Arial, sans-serif">假如 </font>typedArray</code> 不是一个贡献的 typed array,则抛出 {{jsxref("TypeError")}}</li>
 <li>如果 <code>index</code> 超出了 <code>typedArray 的边界,则抛出</code> {{jsxref("RangeError")}}</li>
</ul>

<h2 id="示例">示例</h2>

<pre class="brush: js">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</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Shared Memory', '#Atomics.exchange', 'Atomics.exchange')}}</td>
   <td>{{Spec2('Shared Memory')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器支持">浏览器支持</h2>



<p>{{Compat("javascript.builtins.Atomics.exchange")}}</p>

<h2 id="相关">相关</h2>

<ul>
 <li>{{jsxref("Atomics")}}</li>
 <li>{{jsxref("Atomics.compareExchange()")}}</li>
</ul>