# 深拷贝
简易面试版!!
function deepClone(target) {
// is Object???
if (typeof target == "object") {
// array or object
const result = Array.isArray(target) ? [] : {}
for (let key in target) {
if (target.hasOwnProperty(key)) {
// 引用类型需要再次递归
result[key] = typeof target[key] == "object" ? deepClone(target[key]) : target[key]
}
}
return result
}
return target
}
← 发布订阅模式 Promise A+ →