Skip to content

Instantly share code, notes, and snippets.

@raphamorim
Last active August 29, 2015 13:56
Show Gist options
  • Save raphamorim/9120541 to your computer and use it in GitHub Desktop.
Save raphamorim/9120541 to your computer and use it in GitHub Desktop.
A elegant way to clone antoher object in JavaScript
function cloneObject(obj){
if ("object" != typeof obj) return obj;
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
}
return copy;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment