js details

1, js the var a = s = 1 and var a =, s 1 = 1, the difference

function fl() {
  var q = w = 1;  
}
function fl1() {
  var a = 1, s = 1;  
}

 You see the problem yet?

 var q = w = 1 s in the global variable into a function execution after

2、

var a = {n:1}; 
var b = a;  
a.x = a = {n:2}; 
console.log(a.x);// --> undefined 
console.log(b.x);// --> {n: 2}

The above code is very simple, but can do little right;

Here is my understanding: Description: var a = {n: 1}; var b = a; defined herein {n: 1} for the memory address A; b and a point to address A;

 . "": 1, ax = a = {n 2} Because the operator code is evaluated first, it will ax = undefined; this time is the address A {n: 1, x: undefined} ;

 2, the same operator then starts counting from right to left, a = {n: 2}; In this case a change in the address, the value of {n: 2};

 3、执行 a.x = a;这是最关键的点 ,因为a.x已经执行了代表地址A的一个x值,这里可以把a.x直接做为地址A中的x指向位置,所以这里变为地址A {n: 1,x: {n: 2}};但b又指向地址A,所以b.x值为{n:2}

可查看此处: https://www.cnblogs.com/vajoy/p/3703859.html 更好理解

Guess you like

Origin www.cnblogs.com/flxy-1028/p/11229099.html