AngularJS : copy vs extend

后端 未结 4 1869
清歌不尽
清歌不尽 2021-01-13 23:27

Explanation :

we come across some situation in which we need to copy one object to another object. In that case, we probably have two solutions: ang

4条回答
  •  伪装坚强ぢ
    2021-01-14 00:08

    For the copy of the object the following things metters.

    • Object points to same memory location or not

      • Normal copy - Yes
      • Angular copy - No
      • Angular extend - No
      • Angular merge - No
    • Inner object points to the same memory location or not

      • Normal copy - Yes
      • Angular copy - No
      • Angular extend - No
      • Angular merge - No
    • Does copy keep the current child objects or remove that objects

      • Normal copy - Override
      • Angular copy - Override
      • Angular extend - Keep
      • Angular merge - Keep

    Here is the plunker copy for that

    // '=' assignment copy
    console.info('assignment copy');
    var mySource = {'name' : 'sakshi', 'age' : '24', 'obj' :{'key':'value'}}
    var myDest = {oldObj:'old'} //old properties will be override
    myDest = mySource;
    mySource.name = "Rohit";
    console.log(mySource); // Object {name: "Rohit", age: "24", obj: Object}
    console.log(myDest); // Object {name: "sakshi", age: "24", obj: Object}
    console.log(mySource === myDest); // true         //points to same object
    console.log(mySource.obj === myDest.obj); // true //points to same object
    
    
    // angular.copy()
    console.info('angular copy');
    var mySource = {'name' : 'sakshi', 'age' : '24', 'obj' :{'key':'value'}}
    var myDest = {oldObj:'old'} //old properties will be override
    angular.copy(mySource,myDest);
    mySource.name = "Rohit";
    console.log(mySource); // Object {name: "Rohit", age: "24", obj: Object}
    console.log(myDest); // Object {name: "sakshi", age: "24", obj: Object}
    console.log(mySource === myDest); // false //points to different object
    console.log(mySource.obj === myDest.obj); // false //points to different object
    
    // angular.extend()
    console.info('angular extend');
    var mySource = {'name' : 'sakshi', 'age' : '24', 'obj' :{'key':'value'}}
    var myDest = {oldObj:'old'}
    angular.extend(myDest,mySource);
    mySource.name = "Rohit";
    console.log(mySource); // Object {name: "Rohit", age: "24", obj: Object}
    console.log(myDest); // Object {oldObj:'old',name: "sakshi", age: "24", obj: Object}
    mySource.obj.key = '123';
    console.log(myDest.obj.key);
    console.log(mySource === myDest); // false //points to different object
    console.log(mySource.obj === myDest.obj); // True //points to same object
    
    // angular.extend()
    console.info('angular merge');
    var mySource = {'name' : 'sakshi', 'age' : '24', 'obj' :{'key':'value'}}
    var myDest = {oldObj:'old'}
    angular.merge(myDest,mySource);
    mySource.name = "Rohit";
    console.log(mySource); // Object {name: "Rohit", age: "24", obj: Object}
    console.log(myDest); // Object {oldObj:'old',name: "sakshi", age: "24", obj: Object}
    console.log(mySource === myDest); // false //points to different object
    console.log(mySource.obj === myDest.obj); // false //points to different object
    

提交回复
热议问题