/** * 深拷贝 * @param {Object} obj 要拷贝的对象 */ function deepClone(obj = {}) { if (typeof obj !== 'object' || obj == null) { // obj 是 null ,或者不是对象和数组,间接返回 return obj } // 初始化返回后果 let result if (obj instanceof Array) { result = [] } else { result = {} } for (let key in obj) { // 保障 key 不是原型的属性 if (obj.hasOwnProperty(key)) { // 递归调用!!! result[key] = deepClone(obj[key]) } } // 返回后果 return result }