zoukankan      html  css  js  c++  java
  • es6扩展运算符深浅拷贝

    如果数组或对象中的元素只是简单类型,一层对象或者数组,就是深拷贝

    如:

    let staff1 = {name: 'xiaohong',age: 27};
    let staff2 = {...staff};
    staff2.name = 'xiaoming'
    staff1 : {name: 'xiaohong',age: 27}
    staff2:{name: "xiaoming", age: 27}
    

    如果数组或对象中的元素是复杂类型,就是浅拷贝

    let aa = {
    age: 18,
    name: 'aaa',
        address: {
            city: "hangzhou"
        }
    }
    
    let bb = {...aa}
    bb.address.city = 'shanghai'
    
    
    aa: {
            age: 18,
            name: 'aaa',
            address: {
                city: 'shanghai'
            }
        }
    
    bb: {
            age: 18,
            name: 'aaa',
            address: {
                city: 'shanghai'
            }
        }
    

    扩展运算符深复制

    let cc = {
    age: 18,
    name: 'aaa',
        address: {
            city: "hangzhou"
        }
    }
    let dd = {
        ...cc,
        address: {
             ...cc.address   
        }  
    }
    dd.address.city = 'shanghai'
    
    
    cc: {
            age: 18,
            name: 'aaa',
            address: {
                city: 'hangzhou'
            }
        }
    
    dd: {
            age: 18,
            name: 'aaa',
            address: {
                city: 'shanghai'
            }
        }         
  • 相关阅读:
    各种模板
    HNOI2019总结
    WC2019游记
    THUSC2017 Day1题解
    NOIP2018联赛总结
    LOJ2557. 「CTSC2018」组合数问题
    NOI2018游记
    bzoj4671: 异或图
    sg函数小结
    [NOI2011]Noi嘉年华
  • 原文地址:https://www.cnblogs.com/yigexiaojiangshi/p/15568165.html
Copyright © 2011-2022 走看看