YDKJS Scope and Closures Flashcards
Scope is..
a set of rules for storing variables in some location, and for finding those variables at a later time
Is there a compilation step in JavaScript?
Yes, just not well in advanced, as happens in many traditionally-compiled languages.
Traditional three steps of compilation
- Tokenizing/Lexing
- Breaking up a string of characters into meaning chunks to the language called tokens
- Tokenizing: breaking up a stream of characters into tokens, usually using whitespace as a delimiter
- Lexing: similar to tokenizing, but also adds extra content to the token, describing what its role is
- Parsing
- Taking a stream (array) of tokens and turning it into a tree, called an “Abstract Syntax Tree” (AST), that represents the grammatical structure of the program
- Code-generation
- Taking the generated AST and turning that into machine instructions to actually carry out the operations
Since JavaScript compiles right before execution, it has to employ many tricks, like…
JITs, which lazy compile, and hot re-compiling
What is the engine?
It’s responsbile for start-to-finish compilation and execution of our JS program
What is the compiler?
Handles the dirty work of parsing and code-generation
What is the scope?
Collects and maintains a look-up list of all the declared identifiers (variables). Enforces a strict set of rules as to how these are accessibl to currently executing code.
Break down var a = 2;
- Compiler will perform lexing will be performed, to break it into tokens, which will be parsed into a tree.
- Encountering
var a
, compiler asks scope ifa
is already defined for that particular scope collection- If it does, compiler skils this and moves on
- If not, compiler asks scope to declare new variable
a
for that scope collection
- Compiler then code-generates
a=2
for the engine to execute.- The code that engine runs will first ask scope if there is variable
a
accessible in the current scope collection.- If so, engine uses that variable
- if not, engine looks elsewhere
- If engine eventually finds a variable, it will assign the value
2
to it, - If not, engine will raise an error
- The code that engine runs will first ask scope if there is variable
In Summary
- Compiler declares a variable
- Engine looks up variable in Scope and assigns to it, if found
Describe an LHS lookup
- Stands for Left Hand Side Lookup
- Done when a variable appears on the left side of an assignment operation
- Finds the variable container itself, so it can assign a value to it
- Assignment can be done via the
=
operator, or by passing arguments to function parameters
Would a = 2;
cause an LHS or RHS lookup and why?
- LHS reference
- Because we don’t acutally care about the current value of
a
- We only care about finding the variable container as a target for the
= 2
assignment operation
Describe an RHS lookup
- Stands for Right Hand Side lookup
- Is simply a lookup of the value of some variable
1 function foo(a) { 2 console.log( a ); // 2 3 } 4 5 foo( 2 );
Describe all of the LHS and RHS lookups found in this snippet
-
foo
(line 5) - RHS lookup to find the value offoo
, which will be a function - implicit
a = 2
LHS lookup when calling thefoo
function -
console
- RHS lookup to find the object -
log
- property resolution to ensure the method is on the object -
a
(line 2) - RHS lookup to find the value of a - implicit
arg1 = a
LHS mapping the resolveda
RHS lookup’s value toarg1
parameter of thelog
method
Describe nested scope
- Just as a block or function can be nested with another block or function, scopes can be nested in another scope
- If a variable can’t be found in the immediate scope, the engine consults the next outer containing scope, continuing until found, or until the outermost (aka global) scope is reached
What happens when an LHS and RHS look-up fails?
- If an RHS lookup fails to find a variable, anywhere in the nested scopes, the result is a
ReferenceError
being thrown by the engine - If an LHS lookup reaches the global scope without finding the variable, (and the program isn’t in strict mode), then the global scope will create a new variable of that name in the global scope