Quiz
- Can you explain more about global variable under strict mode?
Sure. Suppose you assign to a new (previously unknown) variable, like this:
Then
- In sloppy mode, it creates that variable.
- In strict mode, it complains that you didn't declare that variable.
Generally, we prefer the latter, because the previously unknown variable might be a typo or other error.
If we want to create (declare) a new variable, it's easy enough to use
var
,let
orconst
. - Is there any case that we want to use sloppy mode instead of strict mode
Only if the code we are using is legacy code that was written in sloppy mode.
New code should really
use strict
. - What is the difference between a module and a library?
None. Just synonyms for the idea of a package (another synonym) of code that can add functionality to a program that imports it.
- When debugging, why is the main module not reachable in the console?
Those are the rules of the game. Modules are locked up, inaccessibly (as far as I know).
- Can you explain globalThis? / could you go over globalThis? Thank you
Glad to. There is one name (at least) that exists in all namespaces (modules), and that is
globalThis
.So, if we put some value in
globalThis
, we can access that value from any module. - What’s the difference between loading as a script and as a module?
Module code goes in its own bucket, separate from code in other buckets. This minimizes possible naming conflicts. That's a good thing.
But it also makes the code inaccessible, which can make debugging harder.
-
I don't get what this code is doing. What is stored in App here?
var App = {}; App.trig = trig; App.colors = colors;
This is storing two values in a dictionary. Consider:
In theApp
code above,trig
andcolors
happen to be a dictionaries themselves, but that's incidental. But let's see: - Can we only import parts of variables from an additional module? Are we creating a separate dictionary and importing that dictionary to the main module? Thank you!
I'm not sure what you mean by "parts of variables".
Variables have values and names. We import the names into another module, thereby gaining access to the values.