深拷贝JS递归实现
function deepClone(source){
// 先判断传入的是数组还是对象
let targetObj = source.constructor === Array ? [] : {}
for(let key in source){
if(source.hasOwnProperty(key)){
// 此处有三种情况,为普通对象、数组和一般数据
if(source[key] && typeof source[key] === 'object'){
targetObj[key] = source[key].constructor === Array ? [] : {}
// 递归调用
targetObj[key] = deepClone(source[key])
}else{
// 普通数据直接赋值
targetObj[key] = source[key]
}
}
}
return targetObj
}