uTools-Manuals/docs/javascript/Reference/Operators/Operator_Precedence.html
2019-04-21 11:50:48 +08:00

301 lines
14 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<p><span class="st">运算符的优先级决定了表达式中运算执行的先后顺序</span>,优先级高的运算符最先被执行。</p>
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/expressions-operatorprecedence.html" width="100%"></iframe></div>
<p class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
<h2 id="Associativity" name="Associativity">结合性</h2>
<p>结合性决定了拥有相同优先级的运算符的执行顺序。考虑下面这个表达式:</p>
<pre>a OP b OP c
</code></pre>
<p>左结合(左到右)相当于把左边的子表达式加上小括号<code>(a OP b) OP c</code>,右关联(右到左)相当于<code>a OP (b OP c)</code>。赋值运算符是右关联的,所以你可以这么写:</p>
<pre>a = b = 5;
</code></pre>
<p>结果 <code>a</code> 和 <code>b</code> 的值都会成为5。这是因为赋值运算符的返回结果就是赋值运算符右边的那个值具体过程是<code>b</code>被赋值为5然后<code>a</code>也被赋值为 <code>b=5</code> 的返回值也就是5。</p>
<h2 id="Table" name="Table">汇总表</h2>
<p>下面的表将所有运算符按照优先级的不同从高到低排列。</p>
<table class="fullwidth-table">
<tbody>
<tr>
<th>优先级</th>
<th>运算类型</th>
<th>关联性</th>
<th>运算符</th>
</tr>
<tr>
<td>20</td>
<td><a href="Reference/Operators/Grouping" title="圆括号运算符( ) 用来控制表达式中的运算优先级。"><code>圆括号</code></a></td>
<td>n/a</td>
<td><code>( … )</code></td>
</tr>
<tr>
<td rowspan="4">19</td>
<td><a href="Reference/Operators/Property_Accessors#点符号表示法" title="属性访问器提供了两种方式用于访问一个对象的属性,它们分别是点号和方括号。"><code>成员访问</code></a></td>
<td>从左到右</td>
<td><code>… . …</code></td>
</tr>
<tr>
<td><a href="Reference/Operators/Property_Accessors#括号表示法" title="属性访问器提供了两种方式用于访问一个对象的属性,它们分别是点号和方括号。"><code>需计算的成员访问</code></a></td>
<td>从左到右</td>
<td><code>… [ … ]</code></td>
</tr>
<tr>
<td><a href="Reference/Operators/new" title="new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例。"><code>new</code></a> (带参数列表)</td>
<td>n/a</td>
<td><code>new … ( … )</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgGuide/Functions" title="JavaScript/Reference/Operators/Special_Operators/function_call">函数调用</a></td>
<td>从左到右</td>
<td><code>… ( <var>… </var>)</code></td>
</tr>
<tr>
<td rowspan="1">18</td>
<td><a href="https://developer.mozilla.orgReference/Operators/new" title="JavaScript/Reference/Operators/Special_Operators/new_Operator">new</a> (无参数列表)</td>
<td>从右到左</td>
<td><code>new …</code></td>
</tr>
<tr>
<td rowspan="2">17</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Arithmetic_Operators#Increment" title="JavaScript/Reference/Operators/Arithmetic_Operators">后置递增</a>(运算符在后)</td>
<td colspan="1" rowspan="2">n/a<br/>
 </td>
<td><code>… ++</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Arithmetic_Operators#Decrement" title="JavaScript/Reference/Operators/Arithmetic_Operators">后置递减</a>(运算符在后)</td>
<td><code>… --</code></td>
</tr>
<tr>
<td colspan="1" rowspan="10">16</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Logical_Operators#Logical_NOT">逻辑非</a></td>
<td colspan="1" rowspan="10">从右到左</td>
<td><code>! …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Bitwise_Operators#Bitwise_NOT" title="JavaScript/Reference/Operators/Bitwise_Operators">按位非</a></td>
<td><code>~ …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Arithmetic_Operators#Unary_plus" title="JavaScript/Reference/Operators/Arithmetic_Operators">一元加法</a></td>
<td><code>+ …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Arithmetic_Operators#Unary_negation" title="JavaScript/Reference/Operators/Arithmetic_Operators">一元减法</a></td>
<td><code>- …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Arithmetic_Operators#Increment" title="JavaScript/Reference/Operators/Arithmetic_Operators">前置递增</a></td>
<td><code>++ …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Arithmetic_Operators#Decrement" title="JavaScript/Reference/Operators/Arithmetic_Operators">前置递减</a></td>
<td><code>-- …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/typeof" title="JavaScript/Reference/Operators/Special_Operators/typeof_Operator">typeof</a></td>
<td><code>typeof …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/void" title="JavaScript/Reference/Operators/Special_Operators/void_Operator">void</a></td>
<td><code>void …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/delete" title="JavaScript/Reference/Operators/Special_Operators/delete_Operator">delete</a></td>
<td><code>delete …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await">await</a></td>
<td><code>await …</code></td>
</tr>
<tr>
<td>15</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Arithmetic_Operators#Exponentiation" title="JavaScript/Reference/Operators/Arithmetic_Operators"></a></td>
<td>从右到左</td>
<td><code>… ** …</code></td>
</tr>
<tr>
<td rowspan="3">14</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Arithmetic_Operators#Multiplication" title="JavaScript/Reference/Operators/Arithmetic_Operators">乘法</a></td>
<td colspan="1" rowspan="3">从左到右<br/>
 </td>
<td><code>… * …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Arithmetic_Operators#Division" title="JavaScript/Reference/Operators/Arithmetic_Operators">除法</a></td>
<td><code>… / …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Arithmetic_Operators#Remainder" title="JavaScript/Reference/Operators/Arithmetic_Operators">取模</a></td>
<td><code>… % …</code></td>
</tr>
<tr>
<td rowspan="2">13</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Arithmetic_Operators#Addition" title="JavaScript/Reference/Operators/Arithmetic_Operators">加法</a></td>
<td colspan="1" rowspan="2">从左到右<br/>
 </td>
<td><code>… + …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Arithmetic_Operators#Subtraction" title="JavaScript/Reference/Operators/Arithmetic_Operators">减法</a></td>
<td><code>… - …</code></td>
</tr>
<tr>
<td rowspan="3">12</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Bitwise_Operators" title="JavaScript/Reference/Operators/Bitwise_Operators">按位左移</a></td>
<td colspan="1" rowspan="3">从左到右</td>
<td><code>&lt;&lt; …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Bitwise_Operators" title="JavaScript/Reference/Operators/Bitwise_Operators">按位右移</a></td>
<td><code>&gt;&gt; …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Bitwise_Operators" title="JavaScript/Reference/Operators/Bitwise_Operators">无符号右移</a></td>
<td><code>&gt;&gt;&gt; …</code></td>
</tr>
<tr>
<td rowspan="6">11</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Comparison_Operators#Less_than_operator" title="JavaScript/Reference/Operators/Comparison_Operators">小于</a></td>
<td colspan="1" rowspan="6">从左到右</td>
<td><code>&lt; …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Comparison_Operators#Less_than__or_equal_operator" title="JavaScript/Reference/Operators/Comparison_Operators">小于等于</a></td>
<td><code>&lt;= …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Greater_than_operator" title="JavaScript/Reference/Operators/Comparison_Operators">大于</a></td>
<td><code>&gt; …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Comparison_Operators#Greater_than_or_equal_operator" title="JavaScript/Reference/Operators/Comparison_Operators">大于等于</a></td>
<td><code>&gt;= …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/in" title="JavaScript/Reference/Operators/Special_Operators/in_Operator">in</a></td>
<td><code>… in </code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/instanceof" title="JavaScript/Reference/Operators/Special_Operators/instanceof_Operator">instanceof</a></td>
<td><code>… instanceof </code></td>
</tr>
<tr>
<td rowspan="4">10</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Comparison_Operators#Equality" title="JavaScript/Reference/Operators/Comparison_Operators">等号</a></td>
<td colspan="1" rowspan="4">从左到右<br/>
 </td>
<td><code>… == …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Comparison_Operators#Inequality" title="JavaScript/Reference/Operators/Comparison_Operators">非等号</a></td>
<td><code>… != …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Comparison_Operators#Identity" title="JavaScript/Reference/Operators/Comparison_Operators">全等号</a></td>
<td><code>… === …</code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/Comparison_Operators#Nonidentity" title="JavaScript/Reference/Operators/Comparison_Operators">非全等号</a></td>
<td><code>… !== …</code></td>
</tr>
<tr>
<td>9</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Bitwise_Operators#Bitwise_AND" title="JavaScript/Reference/Operators/Bitwise_Operators">按位与</a></td>
<td>从左到右</td>
<td><code>&amp; …</code></td>
</tr>
<tr>
<td>8</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Bitwise_Operators#Bitwise_XOR" title="JavaScript/Reference/Operators/Bitwise_Operators">按位异或</a></td>
<td>从左到右</td>
<td><code>… ^ …</code></td>
</tr>
<tr>
<td>7</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Bitwise_Operators#Bitwise_OR" title="JavaScript/Reference/Operators/Bitwise_Operators">按位或</a></td>
<td>从左到右</td>
<td><code>… | …</code></td>
</tr>
<tr>
<td>6</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Logical_Operators#Logical_AND" title="JavaScript/Reference/Operators/Logical_Operators">逻辑与</a></td>
<td>从左到右</td>
<td><code>&amp;&amp; …</code></td>
</tr>
<tr>
<td>5</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Logical_Operators#Logical_OR" title="JavaScript/Reference/Operators/Logical_Operators">逻辑或</a></td>
<td>从左到右</td>
<td><code>… || …</code></td>
</tr>
<tr>
<td>4</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Conditional_Operator" title="JavaScript/Reference/Operators/Special_Operators/Conditional_Operator">条件运算符</a></td>
<td>从右到左</td>
<td><code>… ? … : …</code></td>
</tr>
<tr>
<td rowspan="12">3</td>
<td rowspan="12"><a href="https://developer.mozilla.orgReference/Operators/Assignment_Operators" title="JavaScript/Reference/Operators/Assignment_Operators">赋值</a></td>
<td rowspan="12">从右到左</td>
<td><code>… = …</code></td>
</tr>
<tr>
<td><code>… += …</code></td>
</tr>
<tr>
<td><code>… -= …</code></td>
</tr>
<tr>
<td><code>… *= …</code></td>
</tr>
<tr>
<td><code>… /= …</code></td>
</tr>
<tr>
<td><code>… %= …</code></td>
</tr>
<tr>
<td><code>&lt;&lt;= …</code></td>
</tr>
<tr>
<td><code>&gt;&gt;= …</code></td>
</tr>
<tr>
<td><code>&gt;&gt;&gt;= …</code></td>
</tr>
<tr>
<td><code>&amp;= …</code></td>
</tr>
<tr>
<td><code>… ^= …</code></td>
</tr>
<tr>
<td><code>… |= …</code></td>
</tr>
<tr>
<td colspan="1" rowspan="2">2</td>
<td><a href="https://developer.mozilla.orgReference/Operators/yield" title="JavaScript/Reference/Operators/yield">yield</a></td>
<td colspan="1" rowspan="2">从右到左</td>
<td><code>yield </code></td>
</tr>
<tr>
<td><a href="https://developer.mozilla.orgReference/Operators/yield*" title="JavaScript/Reference/Operators/yield">yield*</a></td>
<td><code>yield* …</code></td>
</tr>
<tr>
<td>1</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Spread_operator" title="JavaScript/Reference/Operators/Spread_operator">展开运算符</a></td>
<td>n/a</td>
<td><code>...</code> …</td>
</tr>
<tr>
<td>0</td>
<td><a href="https://developer.mozilla.orgReference/Operators/Comma_Operator" title="JavaScript/Reference/Operators/Comma_Operator">逗号</a></td>
<td>从左到右</td>
<td><code>… , …</code></td>
</tr>
</tbody>
</table>
<p> </p>
</article>