标签:引用类型 amp slice ice gif bsp hal 代码 class
继续不多bb,直接上代码:
含值类型的元素的假拷贝:
let obj = [1, 2] let copyFake = obj // let copyFake = [...obj] copyFake[1] = 20 console.log(obj[1]) // 20 console.log(copyFake[1]) // 20
含值类型的元素的浅拷贝:
let obj = [1, 2] let copyShallow = obj.slice() // let copyShallow = [...obj] copyShallow[1] = 20 console.log(obj[1]) // 2 console.log(copyShallow[1]) // 20
含引用类型元素的浅拷贝:
let obj = [ { name: ‘小白1‘, job: ‘搬砖‘ }, { name: ‘小白2‘, job: ‘拌水泥‘ } ] let copyShallow = obj.slice() // let copyShallow = [...obj] copyShallow[1].name = ‘大白‘ console.log(obj[1].name) // 大白 console.log(copyShallow[1].name) // 大白
含引用类型元素的深拷贝:
let obj = [ { name: ‘小白1‘, job: ‘搬砖‘ }, { name: ‘小白2‘, job: ‘拌水泥‘ } ] let copyDeep = JSON.parse(JSON.stringify(obj)) copyDeep[1].name = ‘大白‘ console.log(obj[1].name) // 小白2 console.log(copyDeep[1].name) // 大白
标签:引用类型 amp slice ice gif bsp hal 代码 class
原文地址:https://www.cnblogs.com/wen233/p/11269270.html