1.不支持function----------------------------------
var obj = {
name: 'sonia',
age: 18,
sub:{
id:1
},
action:function(){
console.log(this.age);
}
}
var obj2 = JSON.parse(JSON.stringify(obj));
obj.sub.id = 100;
console.log(obj2);
console.log(obj);
2.公共方法--------------------------------------------
var Animal={
name: "duobi",
skin: ["red", "green"],
child: {
xxx: "xxx"
},
say: function(){
console.log("I am ", this.name, " skin:", this.skin)
}
}
function deep(dest, obj){
var o = dest;
for (var key in obj) {
if (typeof obj[key] === 'object'){ //判断是不是对象(是不是数组或对象)
//constructor判断类型是数组还是对象
o[key] = (obj[key].constructor===Array)?[]:{};
deep(o[key], obj[key]);
} else {
o[key] = obj[key]
}
}
return o;
};
var x = deep({},Animal);
var y = deep({},Animal);
x.child.xxx = 'aaaaaa';
console.log(x);
console.log(y);
网友评论