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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
---
title: 表达式和运算符
slug: Web/JavaScript/Reference/Operators
tags:
- JavaScript
- Operators
- 概览
translation_of: Web/JavaScript/Reference/Operators
---
<p>{{jsSidebar("Operators")}}<br>
该章节说明了JavaScript语言所有的运算符,表达式和关键字。</p>
<h2 id="表达式和运算符分类">表达式和运算符分类</h2>
<p>左侧工具栏是按字母表排序的列表。</p>
<h3 id="主要表达式">主要表达式</h3>
<p>JavaScript中基本关键字和常用表达式。</p>
<dl>
<dt>{{jsxref("Operators/this", "this")}}</dt>
<dd><code>this</code> 关键字指向函数的执行上下文。</dd>
<dt>{{jsxref("Operators/function", "function")}}</dt>
<dd><code>function</code> 关键字定义了函数表达式。</dd>
<dt>{{jsxref("Operators/class", "class")}}</dt>
<dd><code>class</code> 关键字定义了类表达式。</dd>
<dt>{{jsxref("Operators/function*", "function*")}}</dt>
<dd><code>function*</code> 关键字定义了一个 generator 函数表达式。</dd>
<dt>{{jsxref("Operators/yield", "yield")}}</dt>
<dd>暂停和恢复 generator 函数。</dd>
<dt>{{jsxref("Operators/yield*", "yield*")}}</dt>
<dd>委派给另外一个generator函数或可迭代的对象。</dd>
<dt>{{jsxref("Operators/async_function", "async function")}}</dt>
<dd><code>async function</code> 定义一个异步函数表达式。</dd>
<dt>{{jsxref("Operators/await", "await")}}</dt>
<dd>暂停或恢复执行异步函数,并等待promise的resolve/reject回调。</dd>
<dt>{{jsxref("Global_Objects/Array", "[]")}}</dt>
<dd>数组初始化/字面量语法。</dd>
<dt>{{jsxref("Operators/Object_initializer", "{}")}}</dt>
<dd>对象初始化/字面量语法。</dd>
<dt>{{jsxref("Global_Objects/RegExp", "/ab+c/i")}}</dt>
<dd>正则表达式字面量语法。</dd>
<dt>{{jsxref("Operators/Grouping", "( )")}}</dt>
<dd>分组操作符。</dd>
</dl>
<h3 id="左表达式">左表达式</h3>
<p>左边的值是赋值的目标。</p>
<dl>
<dt>{{jsxref("Operators/Property_accessors", "属性访问符", "", 1)}}</dt>
<dd>成员运算符提供了对对象的属性或方法的访问。<br>
(<code>object.property</code> 和 <code>object["property"]</code>).</dd>
<dt>{{jsxref("Operators/new", "new")}}</dt>
<dd><code>new</code> 运算符创建了构造函数实例。</dd>
<dt>{{JSxRef("Operators/new%2Etarget", "new.target")}}</dt>
<dd>在构造器中,<code>new.target</code> 指向{{jsxref("Operators/new", "new")}}调用的构造器。</dd>
<dt>{{jsxref("Operators/super", "super")}}</dt>
<dd><code>super</code> 关键字调用父类的构造器。</dd>
<dt>{{jsxref("Operators/Spread_operator", "...obj")}}</dt>
<dd>展开运算符可以将一个可迭代的对象在函数调用的位置展开成为多个参数,或者在数组字面量中展开成多个数组元素。</dd>
</dl>
<h3 id="自增和自减">自增和自减</h3>
<p>前置/后置自增运算符和前置/后置自减运算符。</p>
<dl>
<dt>{{jsxref("Operators/Arithmetic_Operators", "A++", "#Increment")}}</dt>
<dd>后置自增运算符。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "A--", "#Decrement")}}</dt>
<dd>后置自减运算符。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "++A", "#Increment")}}</dt>
<dd>前置自增运算符。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "--A", "#Decrement")}}</dt>
<dd>前置自减运算符。</dd>
</dl>
<h3 id="一元运算符">一元运算符</h3>
<p>一元运算符只有一个操作数.</p>
<dl>
<dt>{{jsxref("Operators/delete", "delete")}}</dt>
<dd><code>delete</code> 运算符用来删除对象的属性。</dd>
<dt>{{jsxref("Operators/void", "void")}}</dt>
<dd><code>void</code> 运算符表示表达式放弃返回值。</dd>
<dt>{{jsxref("Operators/typeof", "typeof")}}</dt>
<dd><code>typeof</code> 运算符用来判断给定对象的类型。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "+", "#Unary_plus")}}</dt>
<dd>一元加运算符将操作转换为Number类型。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "-", "#Unary_negation")}}</dt>
<dd>一元减运算符将操作转换为Number类型并取反。</dd>
<dt>{{jsxref("Operators/Bitwise_Operators", "~", "#Bitwise_NOT")}}</dt>
<dd>按位非运算符。</dd>
<dt>{{jsxref("Operators/Logical_Operators", "!", "#Logical_NOT")}}</dt>
<dd>逻辑非运算符。</dd>
</dl>
<h3 id="算术运算符">算术运算符</h3>
<p>算术运算符以二个数值(字面量或变量)作为操作数,并返回单个数值。</p>
<dl>
<dt>{{jsxref("Operators/Arithmetic_Operators", "+", "#Addition")}}</dt>
<dd>加法运算符。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "-", "#Subtraction")}}</dt>
<dd>减法运算符。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "/", "#Division")}}</dt>
<dd>除法运算符。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "*", "#Multiplication")}}</dt>
<dd>乘法运算符。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "%", "#Remainder")}}</dt>
<dd>取模运算符。</dd>
</dl>
<h3 id="关系运算符">关系运算符</h3>
<p>比较运算符比较二个操作数并返回基于比较结果的<code>Boolean</code>值。</p>
<dl>
<dt>{{jsxref("Operators/in", "in")}}</dt>
<dd><code>in运算符用来判断对象是否拥有给定属性。</code>.</dd>
<dt>{{jsxref("Operators/instanceof", "instanceof")}}</dt>
<dd><code>instanceof</code> 运算符判断一个对象是否是另一个对象的实例。</dd>
<dt>{{jsxref("Operators/Comparison_Operators", "<", "#Less_than_operator")}}</dt>
<dd>小于运算符。</dd>
<dt>{{jsxref("Operators/Comparison_Operators", ">", "#Greater_than_operator")}}</dt>
<dd>大于运算符。</dd>
<dt>{{jsxref("Operators/Comparison_Operators", "<=", "#Less_than_or_equal_operator")}}</dt>
<dd>小于等于运算符。</dd>
<dt>{{jsxref("Operators/Comparison_Operators", ">=", "#Greater_than_or_equal_operator")}}</dt>
<dd>大于等于运算符。 </dd>
</dl>
<div class="note"><strong>注意: =></strong> 不是运算符,而是{{jsxref("Functions/Arrow_functions", "箭头函数")}}的表示符。</div>
<h3 id="相等运算符">相等运算符</h3>
<p>如果相等,操作符返回的是布尔类型的true,否则是false。</p>
<dl>
<dt>{{jsxref("Operators/Comparison_Operators", "==", "#Equality")}}</dt>
<dd>相等 运算符.</dd>
<dt>{{jsxref("Operators/Comparison_Operators", "!=", "#Inequality")}}</dt>
<dd>不等 运算符.</dd>
<dt>{{jsxref("Operators/Comparison_Operators", "===", "#Identity")}}</dt>
<dd>全等 运算符.</dd>
<dt>{{jsxref("Operators/Comparison_Operators", "!==", "#Nonidentity")}}</dt>
<dd>非全等 运算符.</dd>
</dl>
<h3 id="位移运算符">位移运算符</h3>
<p>在二进制的基础上对数字进行移动操作</p>
<dl>
<dt>{{jsxref("Operators/Bitwise_Operators", "<<", "#Left_shift")}}</dt>
<dd>按位左移运算符。</dd>
<dt>{{jsxref("Operators/Bitwise_Operators", ">>", "#Right_shift")}}</dt>
<dd>按位右移运算符。</dd>
<dt>{{jsxref("Operators/Bitwise_Operators", ">>>", "#Unsigned_right_shift")}}</dt>
<dd>按位无符号右移运算符。</dd>
</dl>
<h3 id="二进制位运算符">二进制位运算符</h3>
<p>二进制运算符将它们的操作数作为32个二进制位(0或1)的集合,并返回标准的JavaScript数值。</p>
<dl>
<dt>{{jsxref("Operators/Bitwise_Operators", "&", "#Bitwise_AND")}}</dt>
<dd>二进制位与(AND)。</dd>
<dt>{{jsxref("Operators/Bitwise_Operators", "|", "#Bitwise_OR")}}</dt>
<dd>二进制位或(OR)。</dd>
<dt>{{jsxref("Operators/Bitwise_Operators", "^", "#Bitwise_XOR")}}</dt>
<dd>二进制位异或(XOR)。</dd>
</dl>
<h3 id="二元逻辑运算符">二元逻辑运算符</h3>
<p>逻辑运算符典型的用法是用于布尔(逻辑)值运算, 它们返回布尔值。</p>
<dl>
<dt>{{jsxref("Operators/Logical_Operators", "&&", "#Logical_AND")}}</dt>
<dd>逻辑与。</dd>
<dt>{{jsxref("Operators/Logical_Operators", "||", "#Logical_OR")}}</dt>
<dd>逻辑或。</dd>
<dt>{{JSxRef("Operators/Nullish_coalescing_operator", "??")}}</dt>
<dd>空值合并运算符,如果 ?? 前面是 null 或 undefined,取后面的默认值</dd>
</dl>
<h3 id="条件三元运算符">条件(三元)运算符</h3>
<dl>
<dt>{{jsxref("Operators/Conditional_Operator", "(condition ? ifTrue : ifFalse)")}}</dt>
<dd>
<p>条件元素运算符把两个结果中其中一个符合运算逻辑的值返回。</p>
</dd>
</dl>
<h3 id="赋值运算符">赋值运算符</h3>
<p>赋值元素符会将右边的操作数的值分配给左边的操作数,并将其值修改为右边操作数相等的值。</p>
<dl>
<dt>{{jsxref("Operators/Assignment_Operators", "=", "#Assignment")}}</dt>
<dd>赋值运算符。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "*=", "#Multiplication_assignment")}}</dt>
<dd>赋值乘积。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "/=", "#Division_assignment")}}</dt>
<dd>赋值商。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "%=", "#Remainder_assignment")}}</dt>
<dd>赋值求余。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "+=", "#Addition_assignment")}}</dt>
<dd>赋值求和。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "-=", "#Subtraction_assignment")}}</dt>
<dd>赋值求差。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "<<=", "#Left_shift_assignment")}}</dt>
<dd>左位移。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", ">>=", "#Right_shift_assignment")}}</dt>
<dd>右位移。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", ">>>=", "#Unsigned_right_shift_assignment")}}</dt>
<dd>无符号右位移。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "&=", "#Bitwise_AND_assignment")}}</dt>
<dd>赋值与。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "^=", "#Bitwise_XOR_assignment")}}</dt>
<dd>赋值按位异或。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "|=", "#Bitwise_OR_assignment")}}</dt>
<dd>赋值或。</dd>
<dt>{{JSxRef("Operators/Logical_AND_assignment", "&&=")}}</dt>
<dd>逻辑和赋值运算符。</dd>
<dt>{{JSxRef("Operators/Logical_OR_assignment", "||=")}}</dt>
<dd>逻辑或赋值运算符。</dd>
<dt>{{JSxRef("Operators/Logical_nullish_assignment", "??=")}}</dt>
<dd>逻辑空赋值运算符。</dd>
<dt>{{jsxref("Operators/Destructuring_assignment", "[a, b] = [1, 2]")}}<br>
{{jsxref("Operators/Destructuring_assignment", "{a, b} = {a:1, b:2}")}}</dt>
<dd>
<p>解构赋值允许你分配数组或者对象变量的属性通过使用规定的语法,其看起来和数组和对象字面量很相似。</p>
</dd>
<dt>
<h3 id="逗号操作符">逗号操作符</h3>
</dt>
<dt>{{jsxref("Operators/Comma_Operator", ",")}}</dt>
<dd>逗号操作符允许在一个判断状态中有多个表达式去进行运算并且最后返回最后一个表达式的值。</dd>
<dt>
<h3 id="非标准化特性">非标准化特性</h3>
</dt>
<dt>{{JSxRef("Operators/Expression_closures", "Expression closures", "", 1)}} {{non-standard_inline}}{{obsolete_inline(60)}}</dt>
<dd>闭包表达式语法是一个缩写简单的函数。</dd>
<dt>{{JSxRef("Operators/Legacy_generator_function", "Legacy generator function", "", 1)}} {{non-standard_inline}}{{obsolete_inline(58)}}</dt>
<dd>function关键字能用来定义表达式内部未执行完的function的余下功能。 为了能执行function内部余下的代码, 这个function的内部至少包含一个{{jsxref("Operators/yield", "yield")}} 表达式。</dd>
<dt>{{JSxRef("Operators/Array_comprehensions", "[for (x of y) x]")}} {{non-standard_inline}}{{obsolete_inline(58)}}</dt>
<dd>数组解析</dd>
<dt>{{JSxRef("Operators/Generator_comprehensions", "(for (x of y) y)")}} {{non-standard_inline}}{{obsolete_inline(58)}}</dt>
<dd>生成器解析</dd>
</dl>
<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('ES1', '#sec-11', 'Expressions')}}</td>
<td>{{Spec2('ES1')}}</td>
<td>Initial definition</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-11', 'Expressions')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-ecmascript-language-expressions', 'ECMAScript Language: Expressions')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>New: Spread syntax, rest syntax, destructuring assignment, <code>super</code> keyword.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-ecmascript-language-expressions', 'ECMAScript Language: Expressions')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容">浏览器兼容</h2>
<p>{{Compat("javascript.operators")}}</p>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence">运算符优先级</a></li>
</ul>
|