Skip to content

Instantly share code, notes, and snippets.

@geomorillo
Forked from KrystianP/JS: cloneObject
Created June 5, 2017 20:25
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 geomorillo/6b4b42674c103b43216ef0a1729b3772 to your computer and use it in GitHub Desktop.
Save geomorillo/6b4b42674c103b43216ef0a1729b3772 to your computer and use it in GitHub Desktop.
Clone obj in js
function clone(obj) {
if (null == obj || "object" != typeof obj) return obj;
var copy = new 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