« first day (2667 days earlier)      last day (467 days later) » 

9:15 AM
 
lol, computers R stupid
 
The dangers of using global variables.
 
Speaking of which... is there a way to explicitly access a global variable in JS, even if you're in the context of an IIFE that defines a variable with the same name?
 
If it's defined in the global scope and it's not declared with const or let and it's in the browser, then you can use window.yourVariableHere
I would suggest not shadowing variables, though. It's just easier to handle down the line.
 
It's definitely declared with const or let because why the heck wouldn't it be?
 
9:26 AM
In that case, it's not attached to window automatically.
 
I noticed that, because I tried that, and it did not work.
In C++, you have a scope-resolution operator, which you can use to explicitly access something in the global namespace.
 
Might be useful in JS. But it's probably a bit of an edge case, as globals are discouraged in general.
 
Hmm, true.
Except that in JS, everything is an object, and in my experience, it's not at all unusual to have functions defined at global scope, so it'd make good sense to be able to explicitly call those.
 
You could emulate it with having an object where you register the values const myGlobals = {} and then myGlobals.foo = 42. You can add these down the scope chain const myScopeLevel4Variables = {} etc. It might get cumbersome, though.
@CodyGray The modern (like 10 year old) approach to this is to define them in modules and only import what you use. Then if you want to introduce a new function convertCoffeeToCode(), you can import it when you use it. And if for any reason it needs to be defined again, it won't necessarily clash with the old one. Assuming you use them in two different modules.
 
Yeah, I keep hearing reading about modules, but do they work in userscripts?
 
9:32 AM
If both need to be added to the same module, they can be renamed: import convertCoffeeToCode_Irish from './a' and import convertCoffeeToCode_Colombian from './b'
@CodyGray Erm sort of. You can write the userscript with modules but then you need to bundle it together into one file.
Or use the older module pattern and do it by hand.
 
Hmm. I don't really want to have to do automated or manual bundling.
Seems like JS modules just reinvented C's preprocessor :-(
 
That's under the "sort of". Technically - yes. In practice, userscripts don't really know modules.
@CodyGray Basically, yes. I think the preprocessor does a bit more but similar end result, I guess.
Maybe it's better to say that it's bundlers which use like the preprocessor? I am not too well versed in C build chains. Might be the linker, as well?
Essentially if you have import foo from 'bar' in your module, then the bundler will pull the entire bar module and dump it in the same file.
In unrelated news, I'm hungry, so I think I'll hop over to the fast food place and get one
 
@VLAZ Yeah, the preprocessor is far more versatile...
 
(Note: I don't suggest using that code)
 
9:47 AM
It's invalid code, although it'll probably "work".
 
 
2 hours later…
12:09 PM
Is this answer correct? It seems to say the opposite of what I'd expect...
 
 
4 hours later…
4:19 PM
@CodyGray It's indeed the opposite of what I'd expect as well.
With that said, an engine can optimise code 1 to work like code 2. But it's implementation detail. Wouldn't rely on it.
And upon re-reading that page, that's what jonrsharpe already said in a comment. And I've apparently also upvoted the comment.
Oh, I've also commented...
 
🚽
 

« first day (2667 days earlier)      last day (467 days later) »