Last active
October 4, 2015 16:39
-
-
Save yardfarmer/305c916fc48d54cd2235 to your computer and use it in GitHub Desktop.
js deepCopy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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