简体   繁体   English

Javascript增量评估的操作顺序

[英]Javascript increment operation order of evaluation

I know the what the postfix/prefix increment/decrement operators do. 我知道后缀/前缀增量/减量运算符的作用。 And in javascript, this seems to be no different. 在javascript中,这似乎没有什么不同。

While I can guess the outcome of this line easily: 虽然我可以轻松地猜出这一行的结果:

var foo = 10; console.log(foo, ++foo, foo, foo++, foo); 
// output: 10 11 11 11 12

as ++ operators appear within separate expressions. as ++运算符出现在单独的表达式中。

It gets a bit complicated as these operators appears within the same expression: 它变得有点复杂,因为这些运算符出现在同一个表达式中:

var foo = 10; console.log(foo, ++foo + foo++, foo);
// output[1]: 10 22 12
// Nothing unexpected assuming LTR evaluation

var foo = 10; console.log(foo, foo++ + ++foo, foo);
// output[2]: 10 22 12
// What? Ordering is now different but we have the same output.
// Maybe value of foo is evaluated lazily...

var foo = 10; console.log(foo, foo + ++foo, foo);
// output[3]: 10 21 11
// What?! So first 'foo' is evaluated before the increment?

and my question is, how does Javascript (V8 in this case, as I tested these in Chrome) end up evaluating the addition expression in 2nd and 3rd example differently? 我的问题是,Javascript(在这种情况下是V8,我在Chrome中测试过这些)最终会不同地评估第2和第3个例子中的加法表达式?

Why does foo end up evaluating differently than foo++ . 为什么foo最终会以不同于foo++评估。 Isn't postfix ++ supposed to increment after the expression and just evaluate to foo within expression? 是不是postfix ++应该在表达式后递增,只是在表达式中评估为foo

Just look at: 看看:

foo++ + ++foo

Mentally rewrite it to: 在心理上将其重写为:

foo++ →
    addition_lhs = foo  // addition_lhs == 10
    foo += 1            // foo == 11
++foo →
    foo += 1            // foo == 12
    addition_rhs = foo  // addition_rhs == 12

addition_lhs + addition_rhs == 10 + 12 == 22

And foo + ++foo : foo + ++foo

foo →
    addition_lhs = foo  // addition_lhs == 10
++foo →
    foo += 1            // foo == 11
    addition_rhs = foo  // addition_rhs == 11

addition_lhs + addition_rhs == 10 + 11 == 21

So everything is evaluated left to right, including the incrementation. 所以从左到右评估所有内容,包括增量。

The crucial rule to understand is that in JavaScript the whole left hand side (LHS) is executed, and the value memorized, before any operation gets done on the right hand side (RHS). 理解的关键规则是在JavaScript中,在右侧(RHS)完成任何操作之前,执行整个左侧(LHS)并记住值。

You can either confirm the evaluation order by reading the standard or just place a runtime error in your expression and look what happens: 您可以通过阅读标准来确认评估顺序,或者只是在表达式中放置一个运行时错误,看看会发生什么:

alert(1) + alert(2) + (function () { throw Error(); })() + alert(3)

Understand that when you use foo++ you're telling to the "compiler": after you push it to the stack, increment it. 理解当你使用foo++你告诉“编译器”:在你将它推入堆栈之后,增加它。 When you use ++foo you're telling the other way: increment it then push it to the stack. 当你使用++foo你会说另一种方式:增加它然后将其推送到堆栈。 The ++ operator have preference over the + , since the "compiler" read the expression this way (foo++)+(++foo) ++运算符优先于+ ,因为“编译器”以这种方式读取表达式(foo++)+(++foo)

var foo = 10; var foo = 10; console.log(foo, ++foo + foo++, foo); console.log(foo,++ foo + foo ++,foo);

++foo + foo++
   11 + 11

The pre increment sets foo to 11 then adds it to foo again which is still 11, evaluating to 22 before foo is again incremented. 预增量将foo设置为11然后将其再次添加到foo,这仍然是11,在foo再次递增之前评估为22。

var foo = 10; var foo = 10; console.log(foo, foo++ + ++foo, foo); console.log(foo,foo ++ + ++ foo,foo);

foo++ + ++foo
10    +    12

By the time we reach ++foo, the value has already incriminated from foo++ 当我们达到++ foo时,价值已经从foo ++中剔除了

var foo = 10; var foo = 10; console.log(foo, foo + ++foo, foo); console.log(foo,foo + ++ foo,foo);

foo + ++foo
10  +   11

foo is incremented before we add it to foo, thus giving us 10 + 11 foo在我们添加到foo之前递增,因此给我们10 + 11

SUMMARY 摘要

Basically it all depends on what the current value of foo is when you add them together. 基本上这一切都取决于你将它们加在一起时foo的当前值是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM