javascript - Questions about i++
我想大声告诉你
我想大声告诉你 2017-05-19 10:43:31
0
3
640
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.

我想大声告诉你
我想大声告诉你

reply all(3)
滿天的星座

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.

伊谢尔伦

http://stackoverflow.com/ques... Refer to this, although it is java

i++虽然i加1了,但因为后置++,所以执行i=i(此时i指原来的值1),所以就等于是i=1了。相当于i++It’s useless

伊谢尔伦

i++ return value is 1

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template