var a=0;
b=(a=3) (a=4);
alert(a);
alert(b);
Result a=4,b= 7
I would like to ask, why is a 4? Is the assignment operation from right to left?
仅有的幸福2017-06-26 10:57:04
Order of operations:
var a = 0; // a 0
b = (a = 3) + (a = 4);
// a = 3 ----> a为3,整个赋值语句返回3
// a = 4 ----> a为4,整个赋值语句返回4
// 由于返回值的内存和赋值操作用到的a的内存不同,所以b的运算所用的值,只和返回值有关,不受a的值变化的影响,因此,b = 3 + 4 = 7
// 所以,最终a为4,b为7
怪我咯2017-06-26 10:57:04
Assignment operations are combined from right to left. So the first thing is to assign (a=3)+(a=4) to b. However, (a=3)+(a=4) is executed from left to right. So it shows that 3 is assigned to a, and then 4 is assigned to a. So a ends up being 4 and b ends up being 7.
PHP中文网2017-06-26 10:57:04
b=(a=3)+(a=4) This line of code is executed from left to right. When a=3 is executed, 3 is assigned to a. When a=4, 4 is assigned to a. The final value of a is 4.