Shallow And Deep Copy

Shallow Copy

Deep Copy

  • JSON

    function clone(obj) {
        return JSON.parse(JSON.stringify(obj));
    }
    var cloned = clone({ a:1 });
    
  • deepcopy

    • Installation

      npm install deepcopy
      
    • Usage

      • Node

        var deepcopy = require("deepcopy");
        
      • Browser

        <script src="deepcopy.min.js"></script>
        
    • Example

      var base, copy;
      
      base = {
        desserts: [
          { name: "cake"      },
          { name: "ice cream" },
          { name: "pudding"   }
        ]
      };
      
      copy = deepcopy(base);
      base.desserts = null;
      
      console.log(base);
      // { desserts: null } 
      console.log(copy);
      // { desserts: [ { name: 'cake' }, { name: 'ice cream' }, { name: 'pudding' } ] }
      

References

[1] deepcopy@NPM, deepcopy — deep copy for any data

results matching ""

    No results matching ""