Created
June 19, 2014 16:17
-
-
Save hjzheng/a0c311b2cddc90048490 to your computer and use it in GitHub Desktop.
JS浅copy和深copy
This file contains hidden or 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
//深拷贝 | |
function deepCopy(obj){ | |
if(typeof obj != 'object') { | |
return obj; | |
} | |
var newObj = {}; | |
for(var attr in obj) { | |
newObj[attr] = deepCopy(obj[attr]); | |
} | |
return newObj; | |
} | |
//浅拷贝 | |
function copy(obj){ | |
var newObj = {}; | |
for(var attr in obj){ | |
newObj[attr] = obj[attr]; | |
} | |
return newObj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment