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 ?
高洛峰2017-07-05 10:52:36
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.
大家讲道理2017-07-05 10:52:36
Two points:
a = a++ is assigned first and then incremented
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
给我你的怀抱2017-07-05 10:52:36
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
阿神2017-07-05 10:52:36
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
阿神2017-07-05 10:52:36
++ and = problems with the order of operations. It is recommended to take a look at the priority of operation and assignment