Skip to content

Instantly share code, notes, and snippets.

@think2011
Created May 12, 2017 10:14
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 think2011/2eb0708130a6a39a4fa0f753efe3fff7 to your computer and use it in GitHub Desktop.
Save think2011/2eb0708130a6a39a4fa0f753efe3fff7 to your computer and use it in GitHub Desktop.
cloneObj 深拷贝
var cloneObj = function(obj){
var str, newobj = obj.constructor === Array ? [] : {};
if(typeof obj !== 'object'){
return;
} else if(window.JSON){
str = JSON.stringify(obj), //系列化对象
newobj = JSON.parse(str); //还原
} else {
for(var i in obj){
newobj[i] = typeof obj[i] === 'object' ?
cloneObj(obj[i]) : obj[i];
}
}
return newobj;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment