深拷贝的意思是能拷贝对象中的对象,而不会对原对象产生影响。
方法一采用递归
var json={
'name':'liming',
'age':'20',
'height':'160cm',
'friends':{
'name':'jack',
'age':'21',
'height':'180cm'
},
}
function copy(json){//采用递归的方法
var newJson = {};
for(var i in json){
if(typeof json[i] == "object"){
newJson[i] = copy(json[i]);//若是里边有对象,再次调用copy方法
}else {
newJson[i] = json[i];
}
}
return newJson;
}