New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
global
variable shadowed?
#1026
Comments
Note that the behavior of Chrome console/snippet: var window = window;
window; // still the global window
(function () { var window; return window; })(); // undefined Node.js repl: var global = global;
global; // still the global object
(function () { var global; return global; })(); // undefined Node.js file: var global = global;
global; // undefined
(function () { var global; return global; })(); // undefined Notice when the code gets wrapped in a function, |
I'm wondering if this is causing error I have.
i saw this other project doing an import : Azure/azure-sdk-for-js#9880 |
https://github.com/github/fetch/blob/d1d09fb8039b4b8c7f2f5d6c844ea72d8a3cefe6/fetch.js#L1-L5
I'm not sure if I'm reading this wrong, but it seems like the second
global
is shadowed by thevar global
declaration. After hoisting, the equivalent code isBecause of hoisting, the
global
gets evaluated asfalse
in all environments:I think the intent was to access the
global
object, if it exists.We might have to capture it to a variable,
oldGlobal
, first, but the hoisting ofglobal
prevents us from doing this easily.For example, the following wouldn't work because
oldGlobal
andglobal
declarations are hoisted.The only solution I can think of that preserves the variable name,
global
, is to wrap the code in an IIFE to prevent hoisting aboveoldGlobal
. Thetypeof global
is also moved up tooldGlobal
to prevent errors on environments that don'tglobal
defined.An alternative solution is to rename
global
to some other name.The text was updated successfully, but these errors were encountered: