aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/increment/index.html
blob: afe307f389e158baaab967a1f66dde6de54aaa72 (plain)
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
---
title: 自增 (++)
slug: Web/JavaScript/Reference/Operators/Increment
tags:
  - JavaScript
  - Operator
  - Reference
translation_of: Web/JavaScript/Reference/Operators/Increment
---
<div>{{jsSidebar("Operators")}}</div>

<p>自增运算符 (<code>++</code>) 将其操作数递增(加1)并返回一个值。</p>

<div>{{EmbedInteractiveExample("pages/js/expressions-increment.html")}}</div>



<h2 id="语法">语法</h2>

<pre class="syntaxbox notranslate"><strong>Operator:</strong> <var>x</var>++ or ++<var>x</var>
</pre>

<h2 id="描述">描述</h2>

<p>如使用后置(Postfix)自增,操作符在操作数后(例如  <code><var>x</var>++</code>), 操作数将在自增前返回。</p>

<p>如使用前置(Prefix)自增,操作符在操作数前(例如 <code>++<var>x</var></code>), 操作数将先自增后返回。</p>

<h2 id="示例">示例</h2>

<h3 id="后置自增Postfix_increment">后置自增(Postfix increment)</h3>

<pre class="brush: js notranslate">let x = 3;
y = x++;

// y = 3
// x = 4
</pre>

<h3 id="前置自增Prefix_increment">前置自增(Prefix increment)</h3>

<pre class="brush: js notranslate">let a = 2;
b = ++a;

// a = 3
// b = 3
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-postfix-increment-operator', 'Increment operator')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("javascript.operators.increment")}}</p>

<h2 id="相关链接">相关链接</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/Exponentiation">Exponentiation 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>

<div id="gtx-trans" style="position: absolute; left: 126px; top: 21px;">
<div class="gtx-trans-icon"></div>
</div>