JavaScript 中的 ++i 和 i++ 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6378646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:27:04  来源:igfitidea点击:

What's the difference between ++i and i++ in JavaScript

javascript

提问by Mark Sandman

on my quest to learn and improve my JavaScript I came across a script that has a switch / case statement and I noticed some variables are incremented using ++ with the variable before the ++ and then some variables have the ++ after the variable. What's the difference between these? Here's an example of what I'm trying to explain notice the m and y variables.

在我寻求学习和改进我的 JavaScript 的过程中,我遇到了一个带有 switch / case 语句的脚本,我注意到一些变量使用 ++ 递增,并且在 ++ 之前使用变量,然后一些变量在变量之后使用 ++。这些有什么区别?这是我试图解释的一个例子,注意 m 和 y 变量。

 switch(f){
        case 0:{

            ++m;
            if(m==12){
                m=0;
                y++;
            }
            break;
        }
        case 1:{

            --m;
            if(m==-1){
                m=11;
                y--;
            }
            break;
        }
        case 2:{

            ++y;
            break;
        }
        case 3:{

            --y;
            break;
        }
        case 4:{

            break;
        }
        }

回答by Mike Samuel

++ireturns the value of iafter it has been incremented. i++returns the value of ibefore incrementing.

++ii增加后返回的值。 i++返回i递增前的值。

When the ++comes before its operand it is called the "pre-increment" operator, and when it comes after it is called the "post-increment" operator.

当它++出现在其操作数之前时,它被称为“前增量”运算符,当它出现在它之后时,它被称为“后增量”运算符。

This distinction is only important if you do something with the result.

只有当你对结果做一些事情时,这种区别才重要。

var i = 0, j = 0;

alert(++i);  // alerts 1
alert(j++);  // alerts 0

One thing to note though is that even though i++returns the value before incrementing, it still returns the value after it has been converted to a number.

但需要注意的一件事是,即使i++在递增之前返回值,它仍然返回转换为数字后的值。

So

所以

var s = "1";
alert(typeof s++);  // alerts "number"
alert(s);  // alerts 2, not "11" as if by ("1" + 1)

回答by zzzzBov

The same difference as any other c-style ++incrementor.

与任何其他 c 样式++增量器的区别相同。

foo = ++iis the same as:

foo = ++i是相同的:

i = i + 1;
foo = i;

foo = i++is the same as;

foo = i++是相同的;

foo = i;
i = i + 1;

回答by Connor Smith

var i = 0;
alert('i is ' + (++i)); // i is 1

// i is now 1

var i = 0;
alert('i is ' + (i++)); // i is 0

// i is now 1

回答by Flimzy

In JS (as well as C, Perl, and probably a dozen other languages), the ++i operator increments i beforeit evaluates the statement, and i++ increments it after. Same with --.

在 JS(以及 C、Perl 和其他十几种语言)中,++i 运算符评估语句之前增加 i ,而 i++评估语句之后增加它。同--。

Example:

例子:

var i=1;
alert(i++);

Will show "1", but:

将显示“1”,但是:

var i=1;
alert(++i);

Will show "2".

将显示“2”。

回答by Rob Raisch

To illustrate, assuming:

为了说明,假设:

var a = 1;

then

然后

var b = ++a;

results in

结果是

true === (b === 2 && a === 2)

while

尽管

var b = a++;

results in

结果是

true === (b === 1 && a === 2)

回答by erlando

++i is pre-increment, i++ is post-increment.

++i 是前增量,i++ 是后增量。

If you're only incrementing the variable and not using the result at the same time they are equivalent.

如果您只是增加变量而不是同时使用结果,则它们是等效的。

In which case you really should use i += 1 for readability (says Douglas Crockford..)

在这种情况下,您真的应该使用 i += 1 来提高可读性(Douglas Crockford 说..)