aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/decrement/index.html
blob: f405740df30cafff8db4b037e30963b58838ef66 (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
85
---
title: 自减 (--)
slug: Web/JavaScript/Reference/Operators/自减
tags:
  - JavaScript
  - 自减
  - 语法特性
  - 运算符
translation_of: Web/JavaScript/Reference/Operators/Decrement
---
<div>{{jsSidebar("Operators")}}</div>

<p> 自减运算符(<code>--</code>) 将它的操作数减一,然后返回操作数.</p>

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

<div></div>



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

<pre class="syntaxbox notranslate"><strong>操作符:</strong> <var>x</var>-- or --<var>x</var>
</pre>

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

<p>如果使用后缀式,即将操作符放在操作数的后面 (如,<code><var>x</var>--</code>),运算会减一,然后返回减一之前的值。</p>

<p>如果使用前缀式,即将操作符放在操作数的前面 (如,<code>--<var>x</var></code>),运算会减一,然后返回减一之后的值。</p>

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

<h3 id="后缀式">后缀式</h3>

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

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

<h3 id="前缀式">前缀式</h3>

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

// a = 1
// b = 1
</pre>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-postfix-decrement-operator', '自减运算符')}}</td>
  </tr>
 </tbody>
</table>

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



<p>{{Compat("javascript.operators.decrement")}}</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/Increment">Increment 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>