美文网首页
浅拷贝与深拷贝(JavaScript)

浅拷贝与深拷贝(JavaScript)

作者: One_Hund | 来源:发表于2018-09-18 17:35 被阅读0次
    一、预备知识
    • ECMAScript变量包含两种不同数据类型的值:基本数据类型引用数据类型
      基本数据类型:名值存储在栈内存中;
      引用数据类型:名存在栈内存中,值存在于堆内存中,但是栈内存会提供一个引用的地址指向堆内存中的值。
    • 目前基本数据类型有:Boolean、Null、Undefined、Number、String、Symbol,引用数据类型有:Object、Array、Function、RegExp、Date
    • 深拷贝与浅拷贝的概念只存在于引用数据类型
    二、Js自带的深拷贝方法
    1、Array
    • slice()、concat、Array.from()、... 操作符:只能实现一维数组的深拷贝
    var arr1 = [1, 2, [3, 4]], arr2 = arr1.slice();
    console.log(arr1); //[1, 2, [3, 4]]
    console.log(arr2); //[1, 2, [3, 4]]
    arr2[0] = 2 
    arr2[2][1] = 5; 
    console.log(arr1); //[1, 2, [3, 5]]
    console.log(arr2); //[2, 2, [3, 5]]
    
    2、Object
    • Object.assign():只能实现一维对象的深拷贝
    var obj1 = {x: 1, y: 2}, obj2 = Object.assign({}, obj1);
    console.log(obj1) //{x: 1, y: 2}
    console.log(obj2) //{x: 1, y: 2}
    obj2.x = 2; //修改obj2.x
    console.log(obj1) //{x: 1, y: 2}
    console.log(obj2) //{x: 2, y: 2}
    var obj1 = {
        x: 1, 
        y: {
            m: 1
        }
    };
    var obj2 = Object.assign({}, obj1);
    console.log(obj1) //{x: 1, y: {m: 1}}
    console.log(obj2) //{x: 1, y: {m: 1}}
    obj2.y.m = 2; //修改obj2.y.m
    console.log(obj1) //{x: 1, y: {m: 2}}
    console.log(obj2) //{x: 2, y: {m: 2}}
    
    • JSON.parse(JSON.stringify(obj)):可实现多维对象的深拷贝,但会忽略undefined、任意的函数、symbol 值
    var obj1 = {
        x: 1, 
        y: {
            m: 1
        },
        a:undefined,
        b:function(a,b){
          return a+b
        },
        c:Symbol("foo")
    };
    var obj2 = JSON.parse(JSON.stringify(obj1));
    console.log(obj1) //{x: 1, y: {m: 1}, a: undefined, b: ƒ, c: Symbol(foo)}
    console.log(obj2) //{x: 1, y: {m: 1}}
    obj2.y.m = 2; //修改obj2.y.m
    console.log(obj1) //{x: 1, y: {m: 1}, a: undefined, b: ƒ, c: Symbol(foo)}
    console.log(obj2) //{x: 2, y: {m: 2}}
    

    注:进行JSON.stringify()序列化的过程中,undefined、任意的函数以及 symbol 值,在序列化过程中会被忽略(出现在非数组对象的属性值中时)或者被转换成 null(出现在数组中时)。

    由上面可知,JS 提供的自有方法并不能彻底解决Array、Object的深拷贝问题,因此我们应该自己实现。

    三、深拷贝函数简单写法(递归实现)
    function deepClone(obj){
      let result = Array.isArray(obj)?[]:{};
      if(obj && typeof obj === "object"){
        for(let key in obj){
          if(obj.hasOwnProperty(key)){
            if(obj[key] && typeof obj[key] === "object"){
              result[key] = deepClone(obj[key]);
            }else{
              result[key] = obj[key];
            }
          }
        }
      }
      return result;
    }
    
    // 测试用
    var obj1 = {
        x: {
            m: 1
        },
        y: undefined,
        z: function add(z1, z2) {
            return z1 + z2
        },
        a: Symbol("foo"),
        b: [1,2,3,4,5],
        c: null
    };
    var obj2 = deepClone(obj1);
    obj2.x.m = 2;
    obj2.b[0] = 2;
    console.log(obj1);
    console.log(obj2);
    

    但上面的深拷贝方法遇到循环引用,会陷入一个循环的递归过程,从而导致爆栈。如:

    var obj1 = {
        x: 1, 
        y: 2
    };
    obj1.z = obj1;
    var obj2 = deepClone(obj1);
    

    因此需要改进。

    四、深拷贝函数改进(防止循环递归)

    解决因循环递归而暴栈的问题,只需要判断一个对象的字段是否引用了这个对象或这个对象的任意父级即可。

    function deepClone(obj, parent = null){ // 改进(1)
      let result = Array.isArray(obj)?[]:{};
      let _parent = parent;  // 改进(2)
      while(_parent){ // 改进(3)
        if(_parent.originalParent === obj){
          return _parent.currentParent;
        }
        _parent = _parent.parent;
      }
      if(obj && typeof obj === "object"){
        for(let key in obj){
          if(obj.hasOwnProperty(key)){
            if(obj[key] && typeof obj[key] === "object"){
              result[key] = deepClone(obj[key],{ // 改进(4)
                originalParent: obj,
                currentParent: result,
                parent: parent
              });
            }else{
              result[key] = obj[key];
            }
          }
        }
      }
      return result;
    }
    
    // 调试用
    var obj1 = {
        x: 1, 
        y: 2
    };
    obj1.z = obj1;
    var obj2 = deepClone(obj1);
    console.log(obj1); //太长了去浏览器试一下吧~ 
    console.log(obj2); //太长了去浏览器试一下吧~ 
    
    五、深拷贝函数最终版(支持基本数据类型、原型链、RegExp、Date类型)
    function deepClone(obj, parent = null){ 
      let result; // 最后的返回结果
    
      let _parent = parent; // 防止循环引用
      while(_parent){
        if(_parent.originalParent === obj){
          return _parent.currentParent;
        }
        _parent = _parent.parent;
      }
      
      if(obj && typeof obj === "object"){ // 返回引用数据类型(null已被判断条件排除))
        if(obj instanceof RegExp){ // RegExp类型
          result = new RegExp(obj.source, obj.flags)
        }else if(obj instanceof Date){ // Date类型
          result = new Date(obj.getTime());
        }else{
          if(obj instanceof Array){ // Array类型
            result = []
          }else{ // Object类型,继承原型链
            let proto = Object.getPrototypeOf(obj);
            result = Object.create(proto);
          }
          for(let key in obj){ // Array类型 与 Object类型 的深拷贝
            if(obj.hasOwnProperty(key)){
              if(obj[key] && typeof obj[key] === "object"){
                result[key] = deepClone(obj[key],{ 
                  originalParent: obj,
                  currentParent: result,
                  parent: parent
                });
              }else{
                result[key] = obj[key];
              }
            }
          }
        }
      }else{ // 返回基本数据类型与Function类型,因为Function不需要深拷贝
        return obj
      }
      return result;
    }
    
    // 调试用
    function construct(){
        this.a = 1,
        this.b = {
            x:2,
            y:3,
            z:[4,5,[6]]
        },
        this.c = [7,8,[9,10]],
        this.d = new Date(),
        this.e = /abc/ig,
        this.f = function(a,b){
            return a+b
        },
        this.g = null,
        this.h = undefined,
        this.i = "hello",
        this.j = Symbol("foo")
    }
    construct.prototype.str = "I'm prototype"
    var obj1 = new construct()
    obj1.k = obj1
    obj2 = deepClone(obj1)
    obj2.b.x = 999
    obj2.c[0] = 666
    console.log(obj1)
    console.log(obj2)
    console.log(obj1.str)
    console.log(obj2.str)
    

    注:Function类型的深拷贝:

    • bind():使用fn.bind()可将函数进行深拷贝,但因为this指针指向问题而不能使用;
    • eval(fn.toString()):只支持箭头函数,普通函数function fn(){}则不适用;
    • new Function(arg1,arg2,...,function_body):需将参数与函数体提取出来;
      PS:一般也不需要深拷贝Function。

    相关文章

      网友评论

          本文标题:浅拷贝与深拷贝(JavaScript)

          本文链接:https://www.haomeiwen.com/subject/glnunftx.html