乐趣区

关于javascript:js的深克隆

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 }
退出移动版