var cloneObj = function(obj){
var str, newobj = obj.constructor === Array ? [] : {};
if(typeof obj !== 'object'){
return;
} else if(window.JSON){
str = JSON.stringify(obj), //系列化对象
newobj = JSON.parse(str); //还原
} else {
for(var i in obj){
newobj[i] = typeof obj[i] === 'object' ?cloneObj(obj[i]) : obj[i]; }
}
return newobj;
};
var obj = {a: 0, b: 1, c: 2};
var newobj = cloneObj(obj);
//对克隆后的新对象进行成员删除
delete newobj.a;
console.log(obj, arr, newobj, newarr);
//结果: {a: 0, b: 1, c: 2},{b: 1, c: 2}
//深度克隆使得newobj完全独立于obj