JavaScript 增量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6020701/
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
JavaScript increments
提问by Freesn?w
Possible Duplicate:
++someVariable Vs. someVariable++ in Javascript
I know you can add one to a variable simply by doing i++
(assuming i is your variable). This can best be seen when iterating through an array or using it in a "for" statement. After finding some code to use online, I noticed that the for statement used ++i
(as apposed to i++
).
我知道你可以简单地将一个添加到变量中i++
(假设 i 是你的变量)。在遍历数组或在“for”语句中使用它时,可以最好地看到这一点。在网上找到一些要使用的代码后,我注意到使用了 for 语句++i
(与 相关i++
)。
I was wondering if there was any significant difference or if the two are even handled any differently.
我想知道是否有任何显着差异,或者两者的处理方式是否有任何不同。
回答by Naftali aka Neal
Yes there is a bigdifference.
是的,有很大的不同。
var i = 0;
var c = i++; //c = 0, i = 1
c = ++i; //c = 2, i = 2
//to make things more confusing:
c = ++c + c++; //c = 6
//but:
c = c++ + c++; //c = 13
And here is a fiddle to put it all together: http://jsfiddle.net/maniator/ZcKSF/
这是一个把它们放在一起的小提琴:http: //jsfiddle.net/maniator/ZcKSF/
回答by Rafe Kettler
The value of ++i
is i + 1
and the value of i++
is just i
. After either has evaluated, i
is i + 1
. It's a difference in timing, which is why they're often called 'pre-increment' and 'post-increment'. In a for loop, it rarely matters, though.
的值++i
就是i + 1
和的值i++
就是i
。在任一评估之后,i
是i + 1
。这是时间上的差异,这就是为什么它们通常被称为“前增量”和“后增量”的原因。但是,在 for 循环中,它很少重要。
回答by KooiInc
People like Douglas Crockford advise not to use that way of incrementing, amongst other reasons because of what Rafe Kettler described. No matter how experienced you are, sometimes ++i/i++
willsuprise you. The alternative is to simply add 1 to i using i += 1
, readable, understandable and unambiguous.
由于 Rafe Kettler 的描述,Douglas Crockford 等人建议不要使用这种递增方式,其中包括其他原因。不管你多么有经验,有时 ++i/i++
都会让你大吃一惊。另一种方法是简单地将 1 添加到 i using i += 1
,可读,可理解和明确。
回答by Liv
have a look at this link : http://www.w3schools.com/js/js_operators.aspit's post increment versus pre increment. They both end up incrementing the value but one returns the value BEFORE incrementing (++y) and the other one returns the value AFTER (y++). However, it doesn't make any difference when using it in a for loop --
看看这个链接:http: //www.w3schools.com/js/js_operators.asp它是后增量与前增量。它们最终都会增加值,但一个返回值 BEFORE 递增 (++y) 和另一个返回值 AFTER (y++)。但是,在 for 循环中使用它没有任何区别 -
for( var i = 0; i < 100; i++ ) { ... }
is the same as
是相同的
for( var i = 0; i < 100; ++i ) { ... }
回答by pai.not.pi
a=1;
b=1;
c=++a;//the value of a is incremented first and then assigned to c
d=b++;//the value of b is assigned to d first then incremented
now if you print a,b,c,d..the output will be:
现在如果你打印 a,b,c,d.. 输出将是:
2 2 2 1
2 2 2 1
回答by SquidScareMe
++i is called pre-increment and i++ is called post-increment. The difference is when the variable is incremented. Pre-incrementing a variable usually adds 1 and then uses that value, while post-incrementation uses the variable and then increments.
++i 称为前增量,i++ 称为后增量。不同之处在于变量递增时。对变量进行预递增通常先加 1,然后使用该值,而后递增则使用变量然后递增。