Created
October 30, 2022 23:14
-
-
Save DarrenSem/f955968122f10c0447ae9d5a8f2e201d to your computer and use it in GitHub Desktop.
globalThis-tests.js -- everything you ever wondered about globalThis (and possible polyfills) but never thought to ask
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
// globalThis-tests.js -- everything you ever wondered about globalThis (and possible polyfills) but never thought to ask | |
// console.clear(); | |
console.log("\nglobalThis-tests.JS\t", new Date()); | |
/////// no pre-setup (typical one-time kind of usage) | |
// (function (glo) { glo._var_$ = 7; })(typeof globalThis !== "undefined" ? globalThis : typeof global !== "undefined" ? global : this || {}); // .mjs needed additional || {} ... otherwise imo this is a #GoodEnough compromise (and much more clear!) compared to O_O https://mathiasbynens.be/notes/globalthis | |
// console.log("\n? (preferred way) globalThis._var_$ set by calling IIFE, confirm it was set to 7:\t", globalThis._var_$, "\n"); | |
/////// uncomment one pair of lines below to test availability of that specific "candidate" | |
/////// 'FINAL fails' result will vary depending on environment (web, Node, module, worker) | |
// if (typeof XYZglobalThis === "undefined") globalThis = typeof XYZglobal !== "undefined" ? global : +this || {}; | |
// console.log("\n>>> disabled 3 candidates, will use {}:\t", globalThis); // FINAL fails in import() module (as .js or .mjs), AND FAILS in <script type="module" src="X.js or X.mjs">, AND fails in Node (as .js or .mjs), AND fails in web | |
// if (typeof XYZglobalThis === "undefined") globalThis = typeof XYZglobal !== "undefined" ? global : this || {}; | |
// console.log("\n>>> disabled 2 candidates, will use this:\t", globalThis); // FINAL fails in import() module (as .js or .mjs), AND fails in Node (as .js or .mjs), AND fails in <script type="module" src="X.js or X.mjs">, BUT WORKS in web (because "this" exists) | |
// if (typeof XYZglobalThis === "undefined") globalThis = typeof global !== "undefined" ? global : this || {}; | |
// console.log("\n>>> disabled 1 candidate , will use global:\t", globalThis); // FINAL fails in import() module (as .js or .mjs), AND FAILS in <script type="module" src="X.js or X.mjs">, BUT WORKS in Node (even < v12 -- when globalThis was added) | |
// if (typeof globalThis === "undefined") globalThis = typeof global !== "undefined" ? global : this || {}; // .mjs needed additional || {} ... otherwise imo this is a #GoodEnough compromise (and much more clear!) compared to O_O https://mathiasbynens.be/notes/globalthis | |
// console.log("\n>>> disabled 0 candidates, will use globalThis:\t", globalThis); | |
(function (glo) { glo._var_$ = 7; })(globalThis); | |
// preferred way (because it will ALWAYS work) | |
console.log("\n? (preferred way) globalThis._var_$ set by calling IIFE, confirm it was set to 7:\t", globalThis._var_$, "\n"); | |
// FINAL (recommended against, because it might fail -- see '// FINAL fails' notes above) | |
console.log("\n? (FINAL) _var_$ set by calling IIFE, confirm it was set to 7:\t", _var_$, "\n"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment