昨天学到js引用赋值
自己的一些归纳。
如果是基本数据类型的话。。赋值就是赋值
而如果是复杂数据类型的话。。赋值就是引用
比如 :
console.log("基本数据类型");
var a = 1;
var b = 2;
a = b //此时b吧值赋值给了a
console.log(a); //2
console.log(b); //2
a = 3; //这时将a改为3时,b不会变,任然为2
console.log("复杂数据类型");
var a = {name:"xiaoye",sex:"male"};
var b = {name:"zhangshan",sex:"female"};
b = a ; //此时如果a赋值到b,其实是引用传值
console.log(b); //{name:"zhang3",sex:"female"};
b.name = "li4"; //此时b中的name被改动了
console.log(b) //{name:"li4",sex:"female"};//这时b的name为li4
console.log(a) //{name:"li4",sex:"female"};//这里值得注意,同时a的name也会被相应改为li4
//更为注意的是,如果直接赋值一个对象的话,不会被引用
//比如:
var a = {name:"xiaoye",sex:"male"};
var b = {name:"zhang3",sex:"female"};
b = a;
b = {name:"wang5",sex:"female"};
console.log(b) //注意这里的b的值为{name:"wang5",sex:"female"};
console.log(a) //注意这里的a的值为var a = {name:"xiaoye",sex:"male"};//并不会被修改