Skip to content

Instantly share code, notes, and snippets.

@getify
Created January 28, 2010 00:07
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 getify/288295 to your computer and use it in GitHub Desktop.
Save getify/288295 to your computer and use it in GitHub Desktop.
function cloneObj(obj) {
var clone;
if (Object.prototype.toString.call(obj) === "[object Array]") {
clone = [];
for (var i=0, len=obj.length; i<len; i++) {
clone[i] = cloneObj(obj[i]);
}
}
else if (obj == null) {
clone = obj;
}
else if (typeof obj === "object") {
clone = {};
for (var i in obj) {
if (obj.hasOwnProperty(i)) clone[i] = cloneObj(obj[i]);
}
}
else {
clone = obj;
}
return clone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment