Skip to content

Instantly share code, notes, and snippets.

@zba
Created December 26, 2019 07:40
Show Gist options
  • Save zba/fedbba3500a76fd8a7bbd0df922dad4c to your computer and use it in GitHub Desktop.
Save zba/fedbba3500a76fd8a7bbd0df922dad4c to your computer and use it in GitHub Desktop.
// made to be able to wrap window, in case when Localstorage is unavailable
function copy(object) {
try {
return Object.assign({}, object)
} catch(e) {
const result = {};
Object.keys(object).forEach(key=> {
let value;
try {
value = object[key];
}catch(e) {
value = null;
}
if (typeof value !== 'object' || value === null || value===object) {
result[key] = value
} else {
result[key]=copy(value)
}
});
return result;
}
}
// example
// copy(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment