复制传递是符合直觉的,不会相互影响的
let b1 = 1;
let b2 = b1;
b2+=2;
console.log(b1) //1
console.log(b2) //3
比如像array,如果直接赋值,再改变赋值后的变量,连a1都会受到污染,这是因为内存地址也直接赋值导致的
let a1= [];
let a2 = a1;
a2.push(1,2,3);
console.log(a1) //[1,2,3]
console.log(a2) //[1,2,3]
用Object.assign(target,sources)深拷贝的方式就可以了
let a1=[];
let a2 = Object.assign([],a1)
a2.push(1,2,3);
console.log(a1) //[]
console.log(a2) //[1,2,3]