Last active
January 19, 2016 17:32
-
-
Save andygup/c606217af2d220eb36cc to your computer and use it in GitHub Desktop.
function for doing a static clone of a JavaScript object. Removes all dependencies from original object.
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
/** | |
* Creates a static clone. Removes dependencies between | |
* clone and original object. | |
* @param obj | |
* @return {*} | |
*/ | |
function clone (obj) { | |
if(obj == null || typeof(obj) != "object") { | |
return obj; | |
} | |
try{ | |
var objectClone = {}; | |
for(var key in obj){ | |
objectClone[key] = obj[key]; | |
} | |
} | |
catch(exc){ | |
console.error("Problem encountered while attempting to clone object: " + exc); | |
} | |
return objectClone; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment