简单手写了个深拷贝,记录一下。
function cp(obj) {
function helper(item) {
if(Array.isArray(item)) {
const nar = []
for(const i of item) {
nar.push(helper(i))
}
return nar
} else if (typeof item === 'object') {
const nob = {}
for(const key in item) {
nob[key] = helper(item[key])
}
return nob
} else if (typeof item === 'function') {
return item
} else {
return item
}
}
return helper(obj)
}