// 检查类型
const checkType = data => {
return Object.prototype.toString.call(data).slice(8, -1)
}
const deepClone = target => {
const targetType = checkType(target)
let result = null
if (targetType === 'Object') {
result = {}
} else if (targetType === 'Array') {
result = []
} else {
return target
}
for (const key in target) {
let value = target[key]
const valueType = checkType(value)
if (['Array', 'Object'].includes(valueType)) {
result[key] = deepClone(value)
} else {
result[key] = value
}
}
return result
}
JS深拷贝
最新推荐文章于 2024-03-26 23:45:00 发布