关于javascript:js的深克隆

43次阅读

共计 386 个字符,预计需要花费 1 分钟才能阅读完成。

js 的深克隆

const deepClone = (data)=>{let Obj = Array.isArray(data)?[]:{}
    for(let key in data){if(typeof data[key]==='object'){Obj[key] = deepClone(data[key])
        }else{Obj[key]= data[key]
        }
    }
    return Obj
}
let aa = {
    data:{name:'song'},
    age:18
}
let bb = deepClone(aa)
console.log(bb)
bb.data.name = 'yan'

bb.age = 28
console.log(aa)
console.log(bb)

//{data: { name: 'song'}, age: 18 }
//{data: { name: 'song'}, age: 18 }
//{data: { name: 'yan'}, age: 28 }

正文完
 0