SHARE
TWEET

Clone object (deep copy)

cherokee Jul 23rd, 2014 184 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     var clone = function (obj) {
  2.         var copy = obj instanceof Array? [] : {},
  3.             key;
  4.  
  5.         for (key in obj) {
  6.             if (obj.hasOwnProperty(key)) {
  7.                 if (typeof obj[key] === 'object') {
  8.                     if (obj[key] instanceof Array) {
  9.                         copy[key] = [];
  10.                     } else {
  11.                         copy[key] = {};
  12.                     }
  13.  
  14.                     copy[key] = clone(obj[key]);
  15.                 } else {
  16.                     copy[key] = obj[key];
  17.                 }
  18.             }
  19.         }
  20.  
  21.         return copy;
  22.     };
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