aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/atomics/or/index.html
blob: 286e89cc0a5b01d5d12a851533ccae782bf3d4d9 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
---
title: Atomics.or()
slug: Web/JavaScript/Reference/Global_Objects/Atomics/or
tags:
  - Atomics
  - JavaScript
  - Method
  - Shared Memory
translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/or
---
<div>{{JSRef}}</div>

<p>静态方法 <code><strong>Atomics</strong></code><strong><code>.or()</code></strong> 用数组中指定位置的值进行一次按位或运算,并返回未计算时数组中指定位置处的值。这个atomic操作保证了在修改后的值被写回之前没有其它的写入操作发生。</p>

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



<h2 id="Syntax">Syntax</h2>

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

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

<dl>
 <dt><code>typedArray</code></dt>
 <dd>一个共享的int数组,类型为 {{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> 不是一个可用的int类型,则抛出一个 {{jsxref("TypeError")}} 异常。</li>
 <li>若 <code>typedArray</code> 不是一个共享的数组类型,则抛出一个 {{jsxref("TypeError")}} 异常。</li>
 <li>若 <code>index</code> 索引超出了 <code>typedArray</code> 的大小,则抛出一个 {{jsxref("RangeError")}} 异常。</li>
</ul>

<h2 id="详情">详情</h2>

<p>当 <code>a</code> 或者 <code>b</code> 为1时,按位或运算结果为1。或运算真值表如下:</p>

<table class="standard-table">
 <thead>
  <tr>
   <th><code>a</code></th>
   <th><code>b</code></th>
   <th><code>a | b</code></th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>0</td>
   <td>0</td>
   <td>0</td>
  </tr>
  <tr>
   <td>0</td>
   <td>1</td>
   <td>1</td>
  </tr>
  <tr>
   <td>1</td>
   <td>0</td>
   <td>1</td>
  </tr>
  <tr>
   <td>1</td>
   <td>1</td>
   <td>1</td>
  </tr>
 </tbody>
</table>

<p>例如,让 <code>5 &amp; 1</code> 进行按位或运算的结果是 <code>0101</code> ,也就是十进制的5:</p>

<pre>5  0101
1  0001
   ----
5  0101
</pre>

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

<pre class="brush: js">var sab = new SharedArrayBuffer(1024);
var ta = new Uint8Array(sab);
ta[0] = 2;

Atomics.or(ta, 0, 1); // returns 2, the old value
Atomics.load(ta, 0);  // 3</pre>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">注释</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-atomics.or', 'Atomics.or')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>Initial definition in ES2017.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



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

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

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