Closed smarr closed 6 months ago
I see your point but using just the type without a scope will result in an error when the type is not defined.
what about using globalThis
instead? that should work in every scope
I see your point but using just the type without a scope will result in an error when the type is not defined.
I suppose globalThis
also works, at least on the one Node.js version I tested, yes.
Though, my point was that typeof
is defined to be always safe (as described in the linked documentation). (And strictly speaking there's always the lexical scope.)
Perhaps this example demonstrates that it works: (for convenience https://jsfiddle.net/da1m53y4/)
function nothingDefined() {
return typeof FooBar;
}
function notDefinedAccess() {
return FooBar;
}
function definedAccess() {
function FooBar() {}
return FooBar;
}
function tryIt(fn) {
try {
return fn();
} catch (e) {
return e;
}
}
console.log(tryIt(nothingDefined)); // prints undefined, demonstrating, typeof is safe
console.log(tryIt(notDefinedAccess)); // prints ReferenceError FooBar, as expected
console.log(tryIt(definedAccess)); // and just prints the FooBar
cool didn't know that. thx
Thank you :)
Hi,
I'd like to suggest this change to make your library work on Node.js as it previously did. Some changes of last year introduced the use of
window
, which is not available on Node.js.I'd suggest to use
typeof
as it is always safe: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#interaction_with_undeclared_and_uninitialized_variablesThank you! Stefan