谁来给我解释一下这里发生了什么



我尝试使用chrome调试器工具,但没有得到适当的结果。其输出为(61,16)。我不明白61和16是怎么回事?

let a = 5, b = 15;  
a += ++b + 7 + b++ + b--;

@JsNoob此处,变量值由post-incrementpre-increment更改

这里,看看你的代码:

let a = 5, b = 15; //initial values
//in the second line of your code is...

1. (++b) is here `pre-increment` add 16 here and b value is 16 now.
2. As usual add 7
3. (b++) is here `post-increment` add 16 here and b value is 17 now.  
4. (b--) is here `post-decrement` add 17 here and b value is 16 now. 
5. += is sum and assignment operator.
now the result will be a = 5 + 16 + 7 + 16 + 17 (61)
result:
last value of a is : 61 
&
last value of b is: 16

++b计算前在b中添加1并向b传递新值

b++计算后,在b中添加1,并将新值传递给b

b—从b中计算- 1后,将新值传递给b

a += ++b = 5 + (1+15) = 21

21 + 7 = 2828 + 16 = 44,但b的新值是17

44 + 17 = 61, b的新值为16

a = 61

b = 16

如果a = 5且b = 15则:

A + (b+1) + 7 + (b+1) + (b-1)

基本上:

A + 16 + 7 + 17 + 16

得到56。但是等待。A已经是5了。你把这些数字加到a中,因此:

5 + 56 = 61

还记得我们把变量b加了两次然后减了一次吗?(++b, b++, b——)对于b保持不变。

b + 2-1 (15 + 2 - 1)
b = 16

最新更新