手动实现深拷贝

160 阅读1分钟

1. 简洁版

   缺点:
       1. 无法拷贝日期, 只能将其变成字符串
       2. 无法拷贝undefind, null等
       3. 无法拷贝复杂格式, 如正则等


   let obj = {}
   let obj2 = JSON.parse(JSOM.stringify(obj))
   
   
   

2. 较复杂版

        function deepClone(e, cache){
            // 在第一次进来调用时创建缓存实例, 防止a.self = a 此类情况的出现
            if(!cache){
                cache = new Map()
            }
            if(e instanceof Object){
                // 若此属性内容已被拷贝过则直接拿出缓存中的内容, 不在进行拷贝
                if(cache.get(e)){return cache.get(e)}
                let result = {};
                if(e instanceof Function){
                    if(e.prototype){
                        result = function(){return a.apply(this, arguments)}
                    }else{
                        result = (...args)=>a.call(undefined, ...argus)
                    }
                }
                if(e instanceof Array){
                    result = []
                }
                if(e instanceof Date){
                    // 若是e为日期格式, 则可用e-0的方式将其转换为时间戳格式
                    result = new Date(e-0)
                }
                if(e instanceof RegExp){
                    result = new RegExp(a.source, a.flages)
                }

                cache.set(e,result)

                for(const key in e){
                    // 保证所求key属性是e自己的属性,而不是e原型链上的属性
                    if(e.hasOwnProperty(key))
                    result[key] = deepClone(e[key])
                }

                return result
            }
            return e
        }

3. 不当人版

    function deepClone (data) {
      return new Promise((resolve, reject) => {
        const { port1, port2 } = new MessageChannel()
        port1.postMessage(data)
        port2.onmessage = (res) => {
          resolve(res.data)
        }
      })
    }