<script type="text/javascript">
var obj={ //定义一个数组
name:'liming',
age:"18",
friends:[
"lihong","xiaoli"
]
}
var x;
var obj2 = {};
function copy(obj,obj2){
for(x in obj){
if(typeof obj[x] === "object"){ //判断是否为对象
obj2[x]= (obj[x].constructor===Array)?[]:{}; //判断obj[x]是数组还是对象
copy(obj[x],obj2[x]);
}
else{
obj2[x]=obj[x];
}
}
return obj2;
}
obj=copy(obj,obj2);
obj={name:‘pengpeng’};
console.log(obj);
console.log(obj2);