function deepClone(target) {
if(typeof target !== "object") return target // 退出条件
if(Array.isArray(target)){ // 判断数组
const res = []
for(const i of target){
res.push(Array.isArray(i) ? deepClone(i) : i)
}
return res
} else if(isObject(target)){ // 判断对象
const res = {}
Object.keys(target).forEach(key => {
const v = target[key]
res[key] = isObject(v) ? deepClone(v) : v
})
return res
}
}
function isObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]'
}
const test = [1,2,3,4,{a:1,b:{c:2}}]
console.log(JSON.stringify(deepClone(test)));