if( typeof obj[attr] === 'object'){ newObj[attr] = deepClone(obj[attr]);}
function deepClone(obj){
let newObj = obj.push?[]:{}; //若是obj有push方法則 定義newObj爲數組,不然爲對象。
for(let attr in obj){
if(typeof obj[attr] === 'object'){
newObj[attr] = deepClone(obj[attr]);
}else{
newObj[attr] = obj[attr];
}
}
return newObj;
}
複製代碼