簡體   English   中英

有人請解釋一下這里發生了什么

[英]Someone pls explain me whats happening here

我嘗試使用 chrome 調試器工具,但沒有得到正確的結果。 它的 output 是 (61, 16)。 Unbale理解它 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 = 28 28 + 16 = 44 但 b 的新值為 17

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

一個 = 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM