zoukankan      html  css  js  c++  java
  • ES5 深拷贝&浅拷贝

    浅拷贝

    遍历赋值

     
    const student = {
        name: 'anjiyu',
        sex: 'female',
        studyInfo: {
            school: '河南大学',
            level: '本科',
            posi: '学士'
        }
    }
    function deepCopy(srcObj){
        const resObj = {};
        if(srcObj instanceof Array){
            resObj = [];
        }
        for(const key in srcObj){
            resObj[key] = srcObj[key];
        }
        return resObj;
    }
    
    const newStu = deepCopy(student);
    newStu.studyInfo.school = '河大';
    console.log(`老同学的信息`,student);
    /*
    老同学的信息 {
      name: 'anjiyu',
      sex: 'female',
      studyInfo: { school: '河大', level: '本科', posi: '学士' }
    }
     */
    console.log(`新同学的信息`,newStu);
    /*
    新同学的信息 {
      name: 'anjiyu',
      sex: 'female',
      studyInfo: { school: '河大', level: '本科', posi: '学士' }
    }
    */
    
    
    

    Object.create()

    以传入对象作为原型创建一个新对象,并返回。

     
    const student = {
        name: 'anjiyu',
        sex: 'female',
        studyInfo: {
            school: '河南大学',
            level: '本科',
            posi: '学士'
        }
    }
    
    const newStu = Object.create(student);
    newStu.studyInfo.school = '河大';
    console.log(`老同学的信息`,student);
    /*
    老同学的信息 {
      name: 'anjiyu',
      sex: 'female',
      studyInfo: { school: '河大', level: '本科', posi: '学士' }
    }
     */
    console.log(`新同学的信息`,newStu.__proto__);
    /*
    新同学的信息 {
      name: 'anjiyu',
      sex: 'female',
      studyInfo: { school: '河大', level: '本科', posi: '学士' }
    }
    */
    
    
    

    深拷贝

    递归赋值

     
    const student = {
        name: 'anjiyu',
        sex: 'female',
        studyInfo: {
            school: '河南大学',
            level: '本科',
            posi: '学士'
        }
    }
    function deepCopy(srcObj){
        const resObj = {};
        if(srcObj instanceof Array){
            resObj = [];
        }
        for(const key in srcObj){
            const value = srcObj[key];
            if(typeof value === 'object'){
                resObj[key] = deepCopy(value);
            }else{
                resObj[key] = value;
            }
        }
        return resObj;
    }
    
    const newStu = deepCopy(student);
    newStu.studyInfo.school = '河大';
    console.log(`老同学的信息`,student);
    /*
    老同学的信息 {
      name: 'anjiyu',
      sex: 'female',
      studyInfo: { school: '河南大学', level: '本科', posi: '学士' }
    }
     */
    console.log(`新同学的信息`,newStu);
    /*
    新同学的信息 {
      name: 'anjiyu',
      sex: 'female',
      studyInfo: { school: '河大', level: '本科', posi: '学士' }
    }
    */
    
    
    

    JSON.parse JSON.stringify

    这种方法比较常用

     
    const student = {
        name: 'anjiyu',
        sex: 'female',
        studyInfo: {
            school: '河南大学',
            level: '本科',
            posi: '学士'
        }
    }
    
    const newStu = JSON.parse(JSON.stringify(student));
    newStu.studyInfo.school = '河大';
    console.log(`老同学的信息`,student);
    /*
    老同学的信息 {
      name: 'anjiyu',
      sex: 'female',
      studyInfo: { school: '河南大学', level: '本科', posi: '学士' }
    }
     */
    console.log(`新同学的信息`,newStu.__proto__);
    /*
    新同学的信息 {
      name: 'anjiyu',
      sex: 'female',
      studyInfo: { school: '河大', level: '本科', posi: '学士' }
    }
    */
    
  • 相关阅读:
    CSDN的验证码,真得很糟糕
    CSDN的验证码,为什么要这样呢
    <转>http协议 文件下载原理详解
    验证码,验证码,继续
    搞了一天,气死我了
    昨天下午三点,到晚上六点写的(干死单文档)
    一个早晨加,一个上午的结果
    Linux+QT4+我忙活半宿的结果
    Linux真好玩阿,不过我家电脑不行,运行不够流畅
    springboot之整合JPA
  • 原文地址:https://www.cnblogs.com/HenuAJY/p/14952827.html
Copyright © 2011-2022 走看看