Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active February 13, 2024 14:31
Show Gist options
  • Save rauschma/1bff02da66472f555c75 to your computer and use it in GitHub Desktop.
Save rauschma/1bff02da66472f555c75 to your computer and use it in GitHub Desktop.
/**
* Returns the global object.
* Works even inside ES6 modules.
*/
function getGlobalObject() {
// Workers don’t have `window`, only `self`
if (typeof self !== 'undefined') {
return self;
}
if (typeof global !== 'undefined') {
return global;
}
// Not all environments allow eval and Function
// Use only as a last resort:
return new Function('return this')();
}
@whistler
Copy link

whistler commented Oct 9, 2015

Didn't know that browsers have a window.self property which returns window, neat trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment