Skip to content

Instantly share code, notes, and snippets.

@dreampiggy
Last active June 14, 2016 13:49
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 dreampiggy/daff71c295dafc6e7b47 to your computer and use it in GitHub Desktop.
Save dreampiggy/daff71c295dafc6e7b47 to your computer and use it in GitHub Desktop.
Clone object in JavaScript
'use strict';
function clone(obj) {
if (obj == null || typeof obj != "Object") {
return obj;
}
let copy = obj.constructor();
for (let 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