javascript - Execution process in z=z=z++
滿天的星座
滿天的星座 2017-07-05 10:50:36
0
6
989
var z = 0;
z=z=z++;
alert(z);

The pop-up window is 0, why?

and

var z = 0;
z++;z=z;
alert(z);
The difference between

?

滿天的星座
滿天的星座

reply all(6)
小葫芦

I’m curious whether you are doing this for doing questions or whether you have seen the author using this writing method in the code of an open source project. If it is for doing questions or written tests, I suggest you not consider such a school or company. Because this question is meaningless. , swift3 even removes the ++ operator. Life is short, and it is not worth wasting time on confusing or error-prone syntax features.

大家讲道理

Two points:

  1. a = a++ is assigned first and then incremented

  2. The assignment expression has a return value, which is referred to as the value of the expression

z=z=z++; 

It is equivalent to assigning the value of the expression "z=z++" to z, and the value of "z=z++" is equal to "z++". "z++" first uses the current value of z and then increments it

给我你的怀抱

Post-increment: An expression like
n++ will return a copy of the original value of n, and then the original value of n++
n = n++. The post-increment operator has a higher priority than assignment
So the right side of = will First increment n and return a copy of the original value of n
Then perform an assignment operation to assign the original value of n to n, so the value of n remains unchanged

阿神

z++ is an expression, and the result of the expression is still z, so z=z++ is equivalent to z=z which does nothing.
You need to understand that the logic of z++ is to return the variable first value before incrementing.

Or you can use ++z, the pre-increment is to increment yourself first, and then return the result after the auto-increment

我想大声告诉你

is equivalent to

a=z++;
z=a;
z=z;
阿神

++ and = problems with the order of operations. It is recommended to take a look at the priority of operation and assignment

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