Created
September 25, 2011 17:34
-
-
Save yuanyan/1240875 to your computer and use it in GitHub Desktop.
Object.clone
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
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