Last active
February 13, 2024 14:31
-
-
Save rauschma/1bff02da66472f555c75 to your computer and use it in GitHub Desktop.
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
/** | |
* 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')(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Didn't know that browsers have a window.self property which returns window, neat trick.