++someVariable 与 JavaScript 中的 someVariable++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3469885/
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-08-23 04:46:36  来源:igfitidea点击:

++someVariable vs. someVariable++ in JavaScript

javascriptincrementunary-operator

提问by Derek Adair

In JavaScript you can use ++operator before (pre-increment) or after the variable name (post-increment). What, if any, are the differences between these ways of incrementing a variable?

在 JavaScript 中,您可以在变量名++之前(pre-increment)或之后(post-increment)使用运算符。如果有的话,这些增加变量的方法之间有什么区别?

回答by Jon Skeet

Same as in other languages:

与其他语言相同:

  • ++x(pre-increment) means "increment the variable; the value of the expression is the final value"
  • x++(post-increment) means "remember the original value, then increment the variable; the value of the expression is the original value"
  • ++x(pre-increment) 意思是“增加变量;表达式的值是最终值”
  • x++(post-increment) 意思是“记住原值,然后自增变量;表达式的值就是原值”

Now when used as a standalone statement, they mean the same thing:

现在,当用作独立语句时,它们的意思相同:

x++;
++x;

The difference comes when you use the value of the expression elsewhere. For example:

当您在其他地方使用表达式的值时,差异就会出现。例如:

x = 0;
y = array[x++]; // This will get array[0]

x = 0;
y = array[++x]; // This will get array[1]

回答by Justin Force

  • ++xincrements the value, then evaluates and stores it.
  • x++evaluates the value, then increments and stores it.
  • ++x增加值,然后计算并存储它。
  • x++计算值,然后递增并存储它。
var n = 0, m = 0;

alert(n++); /* Shows 0, then stores n = 1 */
alert(++m); /* Shows 1, then stores m = 1 */

Note that there are slight performance benefits to using ++xwhere possible, because you read the variable, modify it, then evaluate and store it. Versus the x++operator where you read the value, evaluate it, modify it, then store it.

请注意,++x在可能的情况下使用会带来轻微的性能优势,因为您读取变量、修改它,然后评估并存储它。与x++您读取值、评估它、修改它然后存储它的操作符相反。

回答by Chris

As I understand them if you use them standalone they do the same thing. If you try to output the result of them as an expression then they may differ. Try alert(i++) as compared to alert(++i) to see the difference. i++ evaluates to i before the addition and ++i does the addition before evaluating.

据我了解,如果您单独使用它们,它们会做同样的事情。如果您尝试将它们的结果作为表达式输出,那么它们可能会有所不同。尝试将 alert(i++) 与 alert(++i) 进行比较以查看差异。i++ 在加法之前计算为 i 并且 ++i 在评估之前进行加法。

See http://jsfiddle.net/xaDC4/for an example.

有关示例,请参见http://jsfiddle.net/xaDC4/

回答by The Code Guy

var a = 1;
var b = ++a;
alert('a:' + a + ';b:' + b); //a:2;b:2

var c = 1;
var d = c++;
alert('c:' + c + ';d:' + d); //c:2;d:1

jsfiddle

提琴手

回答by cacoder

var x = 0, y = 0;

//post-increment: i++ returns value then adds one to it
console.log('x++ will log: ', x++); //0
console.log('x after x++ : ', x);    //1

//pre-increment: adds one to the value, then returns it
console.log('++y will log: ', ++y); //1
console.log('y after ++y : ', y);   //1