++someVariable 与 someVariable++在 JavaScript 中
在 JavaScript 中,您可以在变量名称之前(预增量)或变量名称之后(后增量)使用 ++
运算符。这些递增变量的方法之间有什么区别(如果有的话)?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
与其他语言相同:
++x
(前自增)表示“变量自增;表达式的值就是最终值”x++
(后自增)意思是“记住原始值,然后递增变量;表达式的值是原始值”现在,当用作独立语句时,它们的含义相同:
当您在其他地方使用表达式的值时,就会出现差异。例如:
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"Now when used as a standalone statement, they mean the same thing:
The difference comes when you use the value of the expression elsewhere. For example:
++x
递增该值,然后计算并存储它。x++
计算该值,然后递增并存储它。请注意,尽可能使用
++x
会带来轻微的性能优势,因为您读取变量,修改它,然后评估并存储它。与x++
运算符不同,您可以在其中读取值、求值、修改它,然后存储它。++x
increments the value, then evaluates and stores it.x++
evaluates the value, then increments and stores it.Note that there are slight performance benefits to using
++x
where possible, because you read the variable, modify it, then evaluate and store it. Versus thex++
operator where you read the value, evaluate it, modify it, then store it.据我了解,如果你单独使用它们,它们会做同样的事情。如果您尝试将它们的结果输出为表达式,那么它们可能会有所不同。尝试将alert(i++) 与alert(++i) 进行比较,看看有什么不同。 i++ 在加法之前计算为 i,而 ++i 在计算之前先进行加法。
有关示例,请参阅 http://jsfiddle.net/xaDC4/。
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.
See http://jsfiddle.net/xaDC4/ for an example.
我有一个关于理解后增量和预增量的解释。所以我把它放在这里。
让我们将
0
分配给x
让我们从后递增开始
为什么?
让我们分解
x++
表达式第一个语句返回
x
的值,即0
稍后当您使用
x
时变量在任意位置,然后执行第二条语句第二条语句返回此
x + 1
表达式的值,即(0 + 1) = 1
请记住 < 的值code>x 处于
1
状态现在让我们从预增量开始
为什么?
让我们分解
++x
表达式第一个语句返回此
x + 1
表达式的值,即(1 + 1) = 2
第二个语句语句返回
x
的值,即2
所以x = 2
因此它返回2
希望这对您有帮助了解什么是后自增和前自增!
I've an explanation of understanding post-increment and pre-increment. So I'm putting it here.
Lets assign
0
tox
Lets start with post-increment
Why?
Lets break the
x++
expression downFirst statement returns the value of
x
which is0
And later when you use
x
variable anywhere, then the second statement is executedSecond statement returns the value of this
x + 1
expression which is(0 + 1) = 1
Keep in mind the value of
x
at this state which is1
Now lets start with pre-increment
Why?
Lets break the
++x
expression downFirst statement returns the value of this
x + 1
expression which is(1 + 1) = 2
Second statement returns the value of
x
which is2
sox = 2
thus it returns2
Hope this would help you understand what post-increment and pre-increment are!
jsfiddle
jsfiddle
如果可能的话,使用
++i
会更清晰、更快:++i
保证您使用的i
值将保持为除非您更改i
,否则相同i++
允许使用i
的值,该值将在“不久的将来”发生变化,如果可能的话,这是不可取的当然,速度并没有快多少,只是快了一点点。
It is clearer and faster to use
++i
if possible :++i
guarantees that you are using a value ofi
that will remains the same unless you changei
i++
allows to use a value ofi
which will change in the "near future", it is not desirable if possibleOf course, it's not really much faster, only a little.