Skip to content

Instantly share code, notes, and snippets.

@yuanyan
Created September 25, 2011 17:34
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 yuanyan/1240875 to your computer and use it in GitHub Desktop.
Save yuanyan/1240875 to your computer and use it in GitHub Desktop.
Object.clone
Object.clone(obj,deep){
if(obj == null || typeof(obj) != 'object')
return obj;
if(deep){
var temp = obj.constructor(); // changed
for(var key in obj)
temp[key] = clone(obj[key]);
return temp;
}else{
function cloned(){}
cloned.prototype = obj;
return new cloned;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment