function deepClone(obj = {}) {
//递归结束的条件 只要递归递到值类型就返回
if (typeof obj !== 'object' || obj == null) return obj
//初始化返回结果
let result
if(obj instanceof Array) result = []
else result = {}
//遍历递归调用 复制树根
for (let i in obj) {
if (obj.hasOwnProperty(i)) {
result[i] = deepClone(obj[i]) //键值对递归
}
}
return result
}
手写深拷贝
最新推荐文章于 2022-05-13 14:27:35 发布