function deepCopy(p, c) {
var c = c || (p instanceof Array?[]:{});
for (var i in p) {
if(!p.hasOwnProperty(i)){ //只对本身属性和方法进行copy
continue;
}
if (p[i] && typeof p[i] === 'object') { //Object or Array except null
c[i] = (p[i].constructor === Array) ? [] : {};
deepCopy(p[i], c[i]);
} else {
c[i] = p[i];
}
}
return c;
}
var xm = {
name:"小明",
age:18,
lessons:['数学','语文','英语'],
brothers:[{
name:"大明",
age:19
},{
name:"小小明",
age:17
}],
sayHi:function(){
console.log("hi,我是小明~");
},
girlFriends:null
};
var nObj = deepCopy(xm);
console.log(nObj);
网友评论