Glad to. It's similar to the Python method of the same
name: dic.keys() returns a sequence of the keys in the
given dictionary. The equivalent in JS
is Object.keys(dic) which is a method on a global
variable called Object. It also returns a list of the
keys. Here's an example:
In practice, as in Python, we would typically use that information to iterate over the object and do stuff with it, say, print it out:
Yes! The "inside" or "execution context" of a function creates a new namespace, extending the global or existing namespace.
Closure variables are non-local, non-global names in those inner namespaces.
Modules are all about global names.
Yes, that's right. The module has to explicitly export the name.
This is different from Python, where you can get access to everything, even if maybe you shouldn't.
Reasonable people could disagree on which is better: Is Python too trusting? Is JS too suspicious?
But, those are the (different) rules of the game.
You can read more than you ever wanted to know about globalThis.
The key idea is that it was introduced to create a standard way of accessing the global environment/namespace.
It's useful to us because it allows code in a module to set global variables that aren't restricted to that module.
Honestly, globalThis is a violation of the separation
that modules are intended for. It's a loophole. Use it sparingly and thoughtfully.
I would really only use it for debugging. For production code, I would stick to defined interfaces.
It patches what many people think was a mistake in the overly permissive early versions of JS.
Consider the assignment statement inside the function foo:
Which of the following is it?
asn?
ans?
Option 1 is the original JS. Option 2 is strict mode.
Great!