实现一
function deepClone(obj){var newObj=obj instanceof Array ? [] : {};
for(let key in obj){newObj[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]
}
return newObj
}
实现二
思考到内置包装对象 Date、RegExp
function deepClone(obj){if(obj === null){return obj}
if(typeof obj !== 'object'){return obj}
if(obj instanceof Date){return new Date(obj)
}
if(obj instanceof RegExp){return new RegExp(obj)
}
// 非 null 状况下的 typeof obj === 'object' 解决形式
var newObj=new obj.constructor;
for(let key in obj){newObj[key]=deepClone(obj[key]);
}
return newObj
}