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'){ //判斷是不是對象(是不是數(shù)組或?qū)ο?
//constructor判斷類型是數(shù)組還是對象
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);
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者