Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active January 2, 2020 15:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rauschma/bb3e5047a461961cfe19a1335b772d7c to your computer and use it in GitHub Desktop.
Save rauschma/bb3e5047a461961cfe19a1335b772d7c to your computer and use it in GitHub Desktop.
// Based on: https://mathiasbynens.be/notes/globalthis
export const getGlobalThis = () => {
if (typeof globalThis !== 'undefined') return globalThis;
if (typeof self !== 'undefined') return self;
if (typeof window !== 'undefined') return window;
if (typeof global !== 'undefined') return global;
return eval.call(undefined, 'this');
};
export const installGlobalThis = () => {
if (typeof globalThis === 'undefined') {
const gt = getGlobalThis();
gt['globalThis'] = gt;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment