js 对象深拷贝

    function deepObj(obj){
      var dest={};   
        for(var key in obj){
          if(typeof obj[key]==='object'){
          dest[key]=obj[key].constructor===Array?[]:{};
          deepObj(dest[key],obj[key]);
        }else{
          dest[key]=obj[key]
        }
      }
      return dest;
    }
    var obj={name:'aa',addr:'werwer',arr:[{name12:'aa',addr12:'werwer'}]}
    var des=deepObj(obj)
    des.arr.name12='aaaaa'
    console.log(des)
    console.log(obj)

  

posted @ 2023-11-15 16:39  howhy  阅读(10)  评论(0编辑  收藏  举报