SHARE
TWEET

Deep Copy of an Object

aslv Jul 23rd, 2014 209 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // BOTH WAYS WORK !!! :)
  2. function clone(obj) {
  3.         return Object.create(obj);
  4.         //return JSON.parse(JSON.stringify(obj));
  5. }
  6.  
  7. function compareObjects(obj, objCopy) {
  8.         console.log('a == b --> ' + (obj == objCopy));
  9. }
  10.  
  11. var a = { name: 'Pesho', age: 21 };
  12. var b = clone(a); // a deep copy
  13. compareObjects(a, b);
  14.  
  15. var a = { name: 'Pesho', age: 21 };
  16. var b = a; // not a deep copy
  17. compareObjects(a, b);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top