1 - JSON.parse 和 JSON.stringify
这种方式可以将数据类型进行转换,使引用地址完全脱离关系,完成深拷贝
- 缺点:不可以拷贝 undefined , function, RegExp 等等类型的
const o = { name: 'xhl', age: 24 }
const newO = JSON.parse(JSON.stringify(o))
2 - 利用递归实现深拷贝
①:
function deepCopy(target) {
// 当检测目标为 基本数据类型、function、Date、RegExp 时,直接返回(首次判断及递归时的拦截)
if (
typeof target !== 'object' ||
target === null ||
target instanceof Date ||
target instanceof RegExp
) return target
// 判断目标时数组还是对象
const newTarget = Array.isArray(target) ? [] : {}
// 遍历对象(数组则遍历下标)将引用类型递归遍历,基本类型直接赋值到新对象(数组)的对应的键上
Object.keys(target).forEach(
key => (newTarget[key] = target[key] instanceof Object ? deepCopy(target[key]) : target[key])
)
// 返回深拷贝后的对象
return newTarget
}
②:使用Object.prototype.toString.call
function deepCopy(target) {
if (
Object.prototype.toString.call(target) !== '[object Object]' &&
Object.prototype.toString.call(target) !== '[object Array]'
) {
return target
}
const newTarget = Array.isArray(target) ? [] : {}
Object.keys(target).forEach(
(key) =>
(newTarget[key] =
target[key] instanceof Object ? deepCopy(target[key]) : target[key])
)
return newTarget
}