/**
* 深度拷贝对象
* @param obj
* @returns {*}
*/
const myDeepCopy = function (obj) {
if (typeof obj !== 'object') {
return obj
}
let newObj = {}
for (let attr in obj) {
newObj[attr] = myDeepCopy(obj[attr])
}
return newObj
}
本文介绍了一个简单的深度拷贝方法,该方法适用于JavaScript对象,并通过递归遍历源对象的所有属性来创建一个新的、独立的副本。
/**
* 深度拷贝对象
* @param obj
* @returns {*}
*/
const myDeepCopy = function (obj) {
if (typeof obj !== 'object') {
return obj
}
let newObj = {}
for (let attr in obj) {
newObj[attr] = myDeepCopy(obj[attr])
}
return newObj
}
7640

被折叠的 条评论
为什么被折叠?