zoukankan      html  css  js  c++  java
  • JS实现深拷贝

    引用类型如果直接将它赋值给另一个变量,由于这两个引用指向同一个地址,这时改变其中任何一个引用,另一个会受到影响,使用深拷贝可以解决这个问题

      递归方法

        function deepClone(data){
           var type = Object.prototype.toString.call(data);
           var obj;
           if(type === 'array'){
               obj = [];
           } else if(type === 'object'){
               obj = {};
           }
    
           if(type === 'array'){
               for(var i = 0, len = data.length; i < len; i++){
                   obj.push(deepClone(data[i]));
               }
           } else if(type === 'object'){
               for(var key in data){
                   obj[key] = deepClone(data[key]);
               }
           }
           return obj;
       }
  • 相关阅读:
    LeetCode 104
    LeetCode 100
    LeetCode 27
    LeetCode 7
    LeetCode 8
    蘑菇街2017春招笔试
    codeforces 5D
    codeforces 5C
    codeforces 875B
    codeforces 876B
  • 原文地址:https://www.cnblogs.com/rlann/p/8495467.html
Copyright © 2011-2022 走看看