Skip to content

Instantly share code, notes, and snippets.

@yardfarmer
Last active October 4, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yardfarmer/305c916fc48d54cd2235 to your computer and use it in GitHub Desktop.
Save yardfarmer/305c916fc48d54cd2235 to your computer and use it in GitHub Desktop.
js deepCopy
var a = 1;
var b = ++a; // b = 2, a = 2;
a = 1;
b = a++; // b = 1; a = 2;
function deepCopy() (parent, child) {
var i,
toStr = Object.propotype.toString,
// x: arrName = '[Object Array]';
arrName = '[object Array]';
// forget check child if null
child = child || {};
for( i in parent ) {
if( parent.hasOwnProerty[i] ) {
if(typeof parent[i] === "object") {
child[i] = (toStr.call(parent[i]) === arrName) ? [] : {};
deepCopy(parent[i],child[i]);
} else {
child[i] = parent[i];
}
}
}
return child;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment