手写深拷贝
function deepClone(data) {
// 如果是函数直接返回,不进行拷贝
if (typeof data === 'function') {
return
}
// 如果是值类型或者是null,返回数据
if (typeof data !== 'object' || data === null) {
return data
}
let obj = {}
if (Array.isArray(data)) {
obj = []
}
// for in 既可以遍历对象也可以遍历数组
for (const key in data) {
if (data.hasOwnProperty(key)) {
// 递归
obj[key] = deepClone(data[key])
}
}
return obj
}