aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/operator_precedence
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/operators/operator_precedence')
-rw-r--r--files/zh-cn/web/javascript/reference/operators/operator_precedence/index.html351
1 files changed, 351 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/operators/operator_precedence/index.html b/files/zh-cn/web/javascript/reference/operators/operator_precedence/index.html
new file mode 100644
index 0000000000..de411354aa
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/operators/operator_precedence/index.html
@@ -0,0 +1,351 @@
+---
+title: 运算符优先级
+slug: Web/JavaScript/Reference/Operators/Operator_Precedence
+tags:
+ - JavaScript
+ - 优先级
+ - 运算符
+translation_of: Web/JavaScript/Reference/Operators/Operator_Precedence
+---
+<div>{{jsSidebar("Operators")}}</div>
+
+<p><span class="st">运算符的优先级决定了表达式中运算执行的先后顺序</span>,优先级高的运算符最先被执行。</p>
+
+<div>{{EmbedInteractiveExample("pages/js/expressions-operatorprecedence.html")}}</div>
+
+
+
+<h2 id="Associativity" name="Associativity">关联性</h2>
+
+<p>关联性决定了拥有相同优先级的运算符的执行顺序。考虑下面这个表达式:</p>
+
+<pre class="notranslate">a OP b OP c;
+</pre>
+
+<p>左关联(左到右)相当于把左边的子表达式加上小括号<code>(a OP b) OP c</code>,右关联(右到左)相当于<code>a OP (b OP c)</code>。赋值运算符是右关联的,所以你可以这么写:</p>
+
+<pre class="notranslate">a = b = 5; </pre>
+
+<p>结果 <code>a</code> 和 <code>b</code> 的值都会成为5。这是因为赋值运算符的返回结果就是赋值运算符右边的那个值,具体过程是:<code>b</code>被赋值为5,然后<code>a</code>也被赋值为 <code>b=5</code> 的返回值,也就是5。</p>
+
+<h2 id="示例">示例</h2>
+
+<pre class="brush: js notranslate">3 &gt; 2 &amp;&amp; 2 &gt; 1
+// return true
+
+3 &gt; 2 &gt; 1
+// 返回 false,因为 3 &gt; 2 是 true,并且 true &gt; 1 is false
+// 加括号可以更清楚:(3 &gt; 2) &gt; 1
+</pre>
+
+<h2 id="Table" name="Table">汇总表</h2>
+
+<p>下面的表将所有运算符按照优先级的不同从高(20)到低(1)排列。</p>
+
+<table class="fullwidth-table">
+ <tbody>
+ <tr>
+ <th>优先级</th>
+ <th>运算类型</th>
+ <th>关联性</th>
+ <th>运算符</th>
+ </tr>
+ <tr>
+ <td>21</td>
+ <td>{{jsxref("Operators/Grouping", "圆括号")}}</td>
+ <td>n/a(不相关)</td>
+ <td><code>( … )</code></td>
+ </tr>
+ <tr>
+ <td rowspan="5">20</td>
+ <td>{{jsxref("Operators/Property_Accessors","成员访问", "#点符号表示法")}}</td>
+ <td>从左到右</td>
+ <td><code>… . …</code></td>
+ </tr>
+ <tr>
+ <td>{{jsxref("Operators/Property_Accessors","需计算的成员访问", "#括号表示法")}}</td>
+ <td>从左到右</td>
+ <td><code>… [ … ]</code></td>
+ </tr>
+ <tr>
+ <td>{{jsxref("Operators/new","new")}} (带参数列表)</td>
+ <td>n/a</td>
+ <td><code>new … ( … )</code></td>
+ </tr>
+ <tr>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Functions" title="JavaScript/Reference/Operators/Special_Operators/function_call">函数调用</a></td>
+ <td>从左到右</td>
+ <td><code>… ( <var>… </var>)</code></td>
+ </tr>
+ <tr>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Optional_chaining">可选链(Optional chaining)</a></td>
+ <td>从左到右</td>
+ <td><code>?.</code></td>
+ </tr>
+ <tr>
+ <td rowspan="1">19</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/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">18</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement" title="JavaScript/Reference/Operators/Arithmetic_Operators">后置递减</a>(运算符在后)</td>
+ <td><code>… --</code></td>
+ </tr>
+ <tr>
+ <td colspan="1" rowspan="10">17</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment" title="JavaScript/Reference/Operators/Arithmetic_Operators">前置递增</a></td>
+ <td><code>++ …</code></td>
+ </tr>
+ <tr>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement" title="JavaScript/Reference/Operators/Arithmetic_Operators">前置递减</a></td>
+ <td><code>-- …</code></td>
+ </tr>
+ <tr>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/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>16</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Exponentiation" title="JavaScript/Reference/Operators/Arithmetic_Operators">幂</a></td>
+ <td>从右到左</td>
+ <td><code>… ** …</code></td>
+ </tr>
+ <tr>
+ <td rowspan="3">15</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Division" title="JavaScript/Reference/Operators/Arithmetic_Operators">除法</a></td>
+ <td><code>… / …</code></td>
+ </tr>
+ <tr>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder" title="JavaScript/Reference/Operators/Arithmetic_Operators">取模</a></td>
+ <td><code>… % …</code></td>
+ </tr>
+ <tr>
+ <td rowspan="2">14</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Subtraction" title="JavaScript/Reference/Operators/Arithmetic_Operators">减法</a></td>
+ <td><code>… - …</code></td>
+ </tr>
+ <tr>
+ <td rowspan="3">13</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators" title="JavaScript/Reference/Operators/Bitwise_Operators">无符号右移</a></td>
+ <td><code>… &gt;&gt;&gt; …</code></td>
+ </tr>
+ <tr>
+ <td rowspan="6">12</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/Operators/instanceof" title="JavaScript/Reference/Operators/Special_Operators/instanceof_Operator">instanceof</a></td>
+ <td><code>… instanceof …</code></td>
+ </tr>
+ <tr>
+ <td rowspan="4">11</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Inequality" title="JavaScript/Reference/Operators/Comparison_Operators">非等号</a></td>
+ <td><code>… != …</code></td>
+ </tr>
+ <tr>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Identity" title="JavaScript/Reference/Operators/Comparison_Operators">全等号</a></td>
+ <td><code>… === …</code></td>
+ </tr>
+ <tr>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Nonidentity" title="JavaScript/Reference/Operators/Comparison_Operators">非全等号</a></td>
+ <td><code>… !== …</code></td>
+ </tr>
+ <tr>
+ <td>10</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_AND" title="JavaScript/Reference/Operators/Bitwise_Operators">按位与</a></td>
+ <td>从左到右</td>
+ <td><code>… &amp; …</code></td>
+ </tr>
+ <tr>
+ <td>9</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_XOR" title="JavaScript/Reference/Operators/Bitwise_Operators">按位异或</a></td>
+ <td>从左到右</td>
+ <td><code>… ^ …</code></td>
+ </tr>
+ <tr>
+ <td>8</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_OR" 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.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_AND" title="JavaScript/Reference/Operators/Logical_Operators">逻辑与</a></td>
+ <td>从左到右</td>
+ <td><code>… &amp;&amp; …</code></td>
+ </tr>
+ <tr>
+ <td>6</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR" title="JavaScript/Reference/Operators/Logical_Operators">逻辑或</a></td>
+ <td>从左到右</td>
+ <td><code>… || …</code></td>
+ </tr>
+ <tr>
+ <td>5</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator" title="JavaScript/Reference/Operators/Nullish_coalescing_operator">空值合并</a></td>
+ <td>从左到右</td>
+ <td><code>… ?? …</code></td>
+ </tr>
+ <tr>
+ <td>4</td>
+ <td><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Conditional_Operator" title="JavaScript/Reference/Operators/Special_Operators/Conditional_Operator">条件运算符</a></td>
+ <td>从右到左</td>
+ <td><code>… ? … : …</code></td>
+ </tr>
+ <tr>
+ <td rowspan="16">3</td>
+ <td rowspan="16"><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Assignment_Operators" title="JavaScript/Reference/Operators/Assignment_Operators">赋值</a></td>
+ <td rowspan="16">从右到左</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>… %= …</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><code>… &amp;&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.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/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.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Comma_Operator" title="JavaScript/Reference/Operators/Comma_Operator">逗号</a></td>
+ <td>从左到右</td>
+ <td><code>… , …</code></td>
+ </tr>
+ </tbody>
+</table>