/**
* 深拷贝
* @param {Object} obj 要拷贝的对象
*/
deepClone(obj = {}) {
if (typeof obj !== 'object' || obj == null) {
// obj 是 null ,或者不是对象和数组,直接返回
return obj
}
// 初始化返回结果
let result
if (obj instanceof Array) {
result = []
} else {
result = {}
}
for (let key in obj) {
// 保证 key 不是原型的属性
if (obj.hasOwnProperty(key)) {
// 递归调用!!!
result[key] = this.deepClone(obj[key])
}
}
return result
}
递归深拷贝
最新推荐文章于 2025-07-30 16:36:32 发布