aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/conflicting/web/javascript/reference/operators/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/conflicting/web/javascript/reference/operators/index.html')
-rw-r--r--files/zh-cn/conflicting/web/javascript/reference/operators/index.html303
1 files changed, 303 insertions, 0 deletions
diff --git a/files/zh-cn/conflicting/web/javascript/reference/operators/index.html b/files/zh-cn/conflicting/web/javascript/reference/operators/index.html
new file mode 100644
index 0000000000..a89cf74368
--- /dev/null
+++ b/files/zh-cn/conflicting/web/javascript/reference/operators/index.html
@@ -0,0 +1,303 @@
+---
+title: 算术运算符
+slug: conflicting/Web/JavaScript/Reference/Operators
+tags:
+ - JavaScript
+ - Operator
+translation_of: Web/JavaScript/Reference/Operators
+translation_of_original: Web/JavaScript/Reference/Operators/Arithmetic_Operators
+original_slug: Web/JavaScript/Reference/Operators/Arithmetic_Operators
+---
+<div>{{jsSidebar("Operators")}}</div>
+
+<p><strong>算术运算符</strong>以数值(字面量或变量)作为其操作数,并返回一个单个数值。标准算术运算符是加法(+),减法(-),乘法(*)和除法(/)。</p>
+
+<div>{{EmbedInteractiveExample("pages/js/expressions-arithmetic.html")}}</div>
+
+
+
+<h2 id="加法">加法 (+)</h2>
+
+<p>加法运算符的作用是数值求和,或者字符串拼接。</p>
+
+<h3 id="语法">语法</h3>
+
+<pre class="syntaxbox notranslate"><strong>运算符:</strong> x + y
+</pre>
+
+<h3 id="示例">示例</h3>
+
+<pre class="brush: js notranslate">// Number + Number -&gt; 数字相加
+1 + 2 // 3
+
+// Boolean + Number -&gt; 数字相加
+true + 1 // 2
+
+// Boolean + Boolean -&gt; 数字相加
+false + false // 0
+
+// Number + String -&gt; 字符串连接
+5 + "foo" // "5foo"
+
+// String + Boolean -&gt; 字符串连接
+"foo" + false // "foofalse"
+
+// String + String -&gt; 字符串连接
+"foo" + "bar" // "foobar"
+</pre>
+
+<h2 id="减法_-">减法 (-)</h2>
+
+<p>减法运算符使两个操作数相减,结果是它们的差值。</p>
+
+<h3 id="语法_2">语法</h3>
+
+<pre class="syntaxbox notranslate"><strong>运算符:</strong> x - y
+</pre>
+
+<h3 id="示例_2">示例</h3>
+
+<pre class="brush: js notranslate">5 - 3 // 2
+3 - 5 // -2
+"foo" - 3 // NaN</pre>
+
+<h2 id="除法">除法 (/)</h2>
+
+<p>除法运算符的结果是操作数的商 ,左操作数是被除数,右操作数是除数。</p>
+
+<h3 id="语法_3">语法</h3>
+
+<pre class="syntaxbox notranslate"><strong>运算符:</strong> x / y
+</pre>
+
+<h3 id="示例_3">示例</h3>
+
+<pre class="brush: js notranslate">1 / 2 // 在 JavaScript 中返回 0.5
+1 / 2 // 在 Java 中返回 0
+// (不需要数字是明确的浮点数)
+
+1.0 / 2.0 // 在 JavaScript 或 Java 中都返回 0.5
+
+2.0 / 0 // 在 JavaScript 中返回 Infinity
+2.0 / 0.0 // 同样返回 Infinity
+2.0 / -0.0 // 在 JavaScript 中返回 -Infinity</pre>
+
+<h2 id="乘法_*">乘法 (*)</h2>
+
+<p>乘法运算符的结果是操作数的乘积。</p>
+
+<h3 id="语法_4">语法</h3>
+
+<pre class="syntaxbox notranslate"><strong>运算符:</strong> x * y
+</pre>
+
+<h3 id="示例_4">示例</h3>
+
+<pre class="brush: js notranslate">2 * 2 // 4
+-2 * 2 // -4
+Infinity * 0 // NaN
+Infinity * Infinity // Infinity
+"foo" * 2 // NaN
+</pre>
+
+<h2 id="求余">求余 (%)</h2>
+
+<p>求余运算符返回第一个操作数对第二个操作数的模,即 <code>var1</code> 对 <code>var2</code> 取模,其中 <code>var1</code> 和 <code>var2</code> 是变量。取模功能就是 <code>var1</code> 除以 <code>var2</code> 的整型余数。</p>
+
+<h3 id="语法_5">语法</h3>
+
+<pre class="syntaxbox notranslate"><strong>运算符:</strong> var1 % var2
+</pre>
+
+<h3 id="示例_5">示例</h3>
+
+<pre class="brush: js notranslate">12 % 5 // 2
+-1 % 2 // -1
+NaN % 2 // NaN
+1 % 2 // 1
+2 % 3 // 2
+-4 % 2 // -0
+5.5 % 2 // 1.5
+</pre>
+
+<h2 id="幂_**">幂 (**)</h2>
+
+<p>幂运算符返回第一个操作数做底数,第二个操作数做指数的乘方。即,<code>var1</code><sup><code>var2</code></sup>,其中 <code>var1</code> 和 <code>var2</code> 是其两个操作数。幂运算符是右结合的。<code>a ** b ** c</code> 等同于 <code>a ** (b ** c)</code>。</p>
+
+<h3 id="语法_6">语法</h3>
+
+<pre class="syntaxbox notranslate"><strong>运算符:</strong> var1 ** var2
+</pre>
+
+<h3 id="注解">注解</h3>
+
+<p>包括 PHP 或 Python 等的大多数语言中,都包含幂运算符(一般来说符号是 ^ 或者 **)。这些语言中的幂运算符有着比其他的单目运算符(如一元 + 或一元 - )更高的优先级。但是作为例外,在 Bash 中,**  运算符被设计为比单目运算符优先级更低。在最新的 JavaScript(ES2016) 中,禁止使用带歧义的幂运算表达式。比如,底数前不能紧跟一元运算符(<code>+/-/~/!/delete/void/typeof</code>)。</p>
+
+<pre class="brush: js notranslate">-2 ** 2;
+// 在 Bash 中等于 4 ,而在其他语言中一般等于 -4
+// 在 JavaScript 中是错误的,因为这会有歧义
+
+-(2 ** 2);
+// -4 在 JavaScript 中能够明显体现出作者的意图</pre>
+
+<h3 id="示例_6">示例</h3>
+
+<pre class="brush: js notranslate">2 ** 3 // 8
+3 ** 2 // 9
+3 ** 2.5 // 15.588457268119896
+10 ** -1 // 0.1
+NaN ** 2 // NaN
+
+2 ** 3 ** 2 // 512
+2 ** (3 ** 2) // 512
+(2 ** 3) ** 2 // 64
+</pre>
+
+<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>
+
+<p>递增运算符为其操作数增加1,返回一个数值。</p>
+
+<ul>
+ <li>如果使用后置(postfix),即运算符位于操作数的后面(如 x++),那么将会在递增前返回数值。</li>
+ <li>如果使用前置(prefix),即运算符位于操作数的前面(如 ++x),那么将会在递增后返回数值。</li>
+</ul>
+
+<h3 id="语法_7">语法</h3>
+
+<pre class="syntaxbox notranslate"><strong>运算符:</strong> x++ 或者 ++x
+</pre>
+
+<h3 id="示例_7">示例</h3>
+
+<pre class="brush: js notranslate">// 后置
+var x = 3;
+y = x++;
+// y = 3, x = 4
+
+// 前置
+var a = 2;
+b = ++a;
+// a = 3, b = 3
+</pre>
+
+<h2 id="递减_--">递减 (--)</h2>
+
+<p>递减运算符将其操作数减去1,并返回一个数值。</p>
+
+<ul>
+ <li>如果后置使用(如 x--),则在递减前返回数值。</li>
+ <li>如果前置使用(如 --x),则在递减后返回数值。</li>
+</ul>
+
+<h3 id="语法_8">语法</h3>
+
+<pre class="syntaxbox notranslate"><strong>运算符:</strong> x-- or --x
+</pre>
+
+<h3 id="示例_8">示例</h3>
+
+<pre class="brush: js notranslate">// 后置
+var x = 3;
+y = x--; // y = 3, x = 2
+
+// 前置
+var a = 2;
+b = --a; // a = 1, b = 1
+</pre>
+
+<h2 id="一元负号_-">一元负号 (-)</h2>
+
+<p>一元负号运算符位于操作数前面,并转换操作数的符号。</p>
+
+<h3 id="语法_9">语法</h3>
+
+<pre class="syntaxbox notranslate"><strong>运算符:</strong> -x
+</pre>
+
+<h3 id="示例_9">示例</h3>
+
+<pre class="brush: js notranslate">var x = 3;
+y = -x; // y = -3, x = 3
+</pre>
+
+<h2 id="一元正号">一元正号 (+)</h2>
+
+<p>一元正号运算符位于其操作数前面,计算其操作数的数值,如果操作数不是一个数值,会尝试将其转换成一个数值。 尽管一元负号也能转换非数值类型,但是一元正号是转换其他对象到数值的最快方法,也是最推荐的做法,因为它不会对数值执行任何多余操作。它可以将字符串转换成整数和浮点数形式,也可以转换非字符串值 <code>true</code>,<code>false</code> <code>和</code> <code>null</code>。小数和十六进制格式字符串也可以转换成数值。负数形式字符串也可以转换成数值(对于十六进制不适用)。如果它不能解析一个值,则计算结果为 <a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NaN">NaN</a>。</p>
+
+<h3 id="语法_10">语法</h3>
+
+<pre class="syntaxbox notranslate"><strong>运算符:</strong> +x
+</pre>
+
+<h3 id="示例_10">示例</h3>
+
+<pre class="brush: js notranslate">+3 // 3
++"3" // 3
++true // 1
++false // 0
++null // 0
++function(val){ return val;} //NaN</pre>
+
+<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')}}</td>
+ <td>{{Spec2('ES1')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-11.3')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td>Defined in several sections of the specification: <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.6">Additive operators</a>, <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.5">Multiplicative operators</a>, <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.3">Postfix expressions</a>, <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.4">Unary operators</a>.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES2015', '#sec-postfix-expressions')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td>Defined in several sections of the specification: <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-additive-operators">Additive operators</a>, <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-multiplicative-operators">Multiplicative operators</a>, <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-postfix-expressions">Postfix expressions</a>, <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-unary-operators">Unary operators</a>.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES2016', '#sec-postfix-expressions')}}</td>
+ <td>{{Spec2('ES2016')}}</td>
+ <td>Added <a href="https://github.com/rwaldron/exponentiation-operator">Exponentiation operator</a>.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES2017', '#sec-postfix-expressions')}}</td>
+ <td>{{Spec2('ES2017')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-additive-operators')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<div class="hidden">
+<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
+</div>
+
+<p>{{Compat("javascript.operators.arithmetic")}}</p>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Assignment_Operators">赋值运算符</a></li>
+</ul>