var a = [1,2,3];
var b = a;
b.push(4);
console.log(a);//[1,2,3,4]
var a = [1,2,3];
var b = a;
b=[1,2,3,4];//改变了引用地址
console.log(a);//[1,2,3]
// 浅拷贝
var a = {};
var b = {name:"hello"};
function copy(obj){
var newObj = {};
for(var attr in obj){
newObj[attr] = obj[attr];
}
return newObj;
}
a = copy(b);
a.name = ‘hi‘;
console.log(b.name)//hi;
//递归
function all(n){
if(n==1){
console.trace(n);
return 1;
}
return n*all(n-1);
}
console.log(all(4))
//递归深拷贝
var a = {};
var b = {name:{age:"hello"}};
function deepcopy(obj){
if(typeof obj!=‘object‘){
console.trace();
return obj;
}
var newObj = {};
for(var attr in obj){
newObj[attr] = deepcopy(obj[attr]);
}
return newObj;
}
var a = deepcopy(b);
a.name.age = ‘hi‘;
console.log(b.name.age)//hello;