javascript中的深拷贝和浅拷贝
var obj = {
name:"jack",
age:18,
gender:"female",
hobbies:{
swim:"游泳",
eat:"各种吃"
}
};
}
var obj1 = {};
for ( var i in obj) {
obj1[i] = obj[i];
}
console.log(obj1)
var obj = {
name:"jack",
age:18,
gender:"female",
isOld : false,
money:undefined,
wife:null,
hobbies:{
swim:"游泳",
eat:"各种吃",
daPai:{
puKe:"各种打",
douDiZhu:"各种斗"
}
}
};
function deepCopy(obj,obj1) {
var key;
for (key in obj) {
if (typeof (obj[key]) != 'object' || obj[key] === null) {
obj1[key] = obj[key];
}else {
obj1[key] = obj[key] instanceof Array? []:{};
deepCopy(obj[key],obj1[key]);
}
}
}
var obj2 = {};
deepCopy(obj,obj2);
obj.name = "rose";
obj.hobbies.swim = "今天不游泳了";
obj.hobbies.daPai.puKe = "打扑克";
console.log(obj);
console.log(obj2);