var i = 1;
var a = i++;
console.log(a); //1
This is understandable.
But this:
var i = 1;
i = i++;
console.log(i); //1
Why is 1
still output here? Even if i = 1
is assigned first, the operation of i
should still be executed, printing i
is still 1
.
滿天的星座2017-05-19 10:45:31
i = i++
Principle:
Take out the value of variable i and put it in a temporary variable.
Increase the value of variable i.
Use the value of the temporary variable as the value of i before the auto-increment operation.
After the above three steps, although the variable i was incremented in the second step, the original value was assigned to it after the third step, so the final output result is 1.
伊谢尔伦2017-05-19 10:45:31
http://stackoverflow.com/ques... Refer to this, although it is java
i++
虽然i加1了,但因为后置++
,所以执行i=i
(此时i指原来的值1),所以就等于是i=1
了。相当于i++
It’s useless