Skip to content

Instantly share code, notes, and snippets.

@andygup
Last active January 19, 2016 17:32
Show Gist options
  • Save andygup/c606217af2d220eb36cc to your computer and use it in GitHub Desktop.
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.
/**
* 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