Skip to content

Instantly share code, notes, and snippets.

@hjzheng
Created June 19, 2014 16:17
Show Gist options
  • Save hjzheng/a0c311b2cddc90048490 to your computer and use it in GitHub Desktop.
Save hjzheng/a0c311b2cddc90048490 to your computer and use it in GitHub Desktop.
JS浅copy和深copy
//深拷贝
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