diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/operators/exponentiation/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/operators/exponentiation/index.html | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/operators/exponentiation/index.html b/files/zh-cn/web/javascript/reference/operators/exponentiation/index.html new file mode 100644 index 0000000000..e9299d7f8f --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/exponentiation/index.html @@ -0,0 +1,107 @@ +--- +title: 求幂 (**) +slug: Web/JavaScript/Reference/Operators/Exponentiation +tags: + - JavaScript + - 参考 + - 语言特征 + - 运算符 +translation_of: Web/JavaScript/Reference/Operators/Exponentiation +--- +<div>{{jsSidebar("Operators")}}</div> + +<p><font><font>求幂运算符(</font></font><code>**</code><font><font>)返回将第一个操作数加到第二个操作数</font><font>的幂</font><font>的结果。</font><font>它等效于</font></font><code>Math.pow</code><font><font>,不同之处在于它也接受BigInts作为操作数。</font></font></p> + +<div>{{EmbedInteractiveExample("pages/js/expressions-exponentiation.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate"><strong>Operator:</strong> <var>var1</var> ** <var>var2</var> +</pre> + +<h2 id="简介">简介</h2> + +<p>求幂运算符是是<strong>右结合的</strong>: <code><var>a</var> ** <var>b</var> ** <var>c</var></code> 等于 <code><var>a</var> ** (<var>b</var> ** <var>c</var>)</code>.</p> + +<p>在大多数语言里,比如PHP、Python等那些有一个幂运算符 (<code>**</code>) 的语言,幂运算符被定义有一个比一元运算符,比如一元的 <code>+</code> 和一元的 <code>-</code> 更高的运算顺序,但有一些例外。在Bash语言里,<code>**</code> 运算符被定义有一个比一元运算符更低的运算顺序。</p> + +<p>在JavaScript里,你不可能写出一个不明确的求幂表达式。这就是说,你不能立刻将一个一元运算符(<code>+/-/~/!/delete/void/typeof</code>)放在基数前,这样做只会导致一个语法错误。</p> + +<pre class="brush: js notranslate">-2 ** 2; +// 4 in Bash, -4 in other languages. +// This is invalid in JavaScript, as the operation is ambiguous. + + +-(2 ** 2); +// -4 in JavaScript and the author's intention is unambiguous. +</pre> + +<p>注意有些编程语言用扬抑符 <kbd>^</kbd> 做乘方运算,但是JavaScript将这个符号作为了<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_XOR">XOR位逻辑运算符</a>。</p> + +<h2 id="例子">例子</h2> + +<h3 id="基本求幂">基本求幂</h3> + +<pre class="brush: js notranslate">2 ** 3 // 8 +3 ** 2 // 9 +3 ** 2.5 // 15.588457268119896 +10 ** -1 // 0.1 +NaN ** 2 // NaN +</pre> + +<h3 id="结合">结合</h3> + +<pre class="brush: js notranslate">2 ** 3 ** 2 // 512 +2 ** (3 ** 2) // 512 +(2 ** 3) ** 2 // 64</pre> + +<h3 id="与一元运算符的用法">与一元运算符的用法</h3> + +<p>取求幂表达式的值的相反数:</p> + +<pre class="brush: js notranslate">-(2 ** 2) // -4 +</pre> + +<p>将求幂表达式的底数转化为一个负数:</p> + +<pre class="brush: js notranslate">(-2) ** 2 // 4 +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-exp-operator', 'Exponentiation operator')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器支持">浏览器支持</h2> + + + +<p>{{Compat("javascript.operators.exponentiation")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Addition">Addition operator</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Subtraction">Subtraction operator</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Division">Division operator</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Multiplication">Multiplication operator</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder">Remainder operator</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment">Increment operator</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Decrement">Decrement operator</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_negation">Unary negation operator</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus">Unary plus operator</a></li> +</ul> + +<p> + <audio style="display: none;"></audio> +</p> |