Last active
June 14, 2016 13:49
-
-
Save dreampiggy/daff71c295dafc6e7b47 to your computer and use it in GitHub Desktop.
Clone object in JavaScript
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
'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