We all know that this in Javascript is a topic where people struggle with. If we now not only think about plain javascript but look in what environ...
For further actions, you may consider blocking this person and/or reporting abuse
They've chosen a poor name there, I think. 'global' implies it's the top level,
this
implies it's local to whatever context you're in. Everyone should be able to access "global" scope, but other modules shouldn't see yourthis
.So how is
globalThis
a name that's going to work? It's another way Javascript is getting more confusing. Wouldn't it be better to suggest not sharing global scope at all?You can read more about it here: github.com/tc39/proposal-global/is...
I thought globalThis has more to do with context of environment. The title of this post is a bit misleading in a way.. kind of,
this
bleeding out to a global scope is an unfortunate accident in my book rather than explicit intention using window or module or global etc. Maybe stop using this to target the global scope.Hmm this is why I added the "for global variables".
I thought this would be enough?
What would be a better title?
'Stop using global variables and if you have to, think about if your code is isomorphic, then if it is use globalThis'. It's a bit wordy. Sorry I think your title is better.
haha, that's the problem with title :D
Coming up with a good title is an art.
And I'm bad at it :D
Quite interesting and good to know, could help quick prototyping, especially if accessible from multiple files.
But then some question, what I did not found at the first search:
I'm curious what people opinion is, especially if we thing on other languages - somewhat - strict general practice what basically start with: 'Do not use global, nor enabled them'.
I think they have introduced a new standard 'globalThis' to be a cross node solution in ES2020
yes :D
That's what I was trying to say in this post.
Yea but since a lot of frameworks and libraries are encouraging us to move to functional paradigm we might not use it in future.Nonetheless it is still a good concept to keep in mind.
Yeah but internal stuff in the frameworks and in specific cases you will need to use it :)
Interesting, didn't know about this. Does it come with any side effects?
Not that I know right now.
If you have global variables, why would you still be using this? It makes no sense