YDKJS Scope and Closures Flashcards

1
Q

Scope is..

A

a set of rules for storing variables in some location, and for finding those variables at a later time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is there a compilation step in JavaScript?

A

Yes, just not well in advanced, as happens in many traditionally-compiled languages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Traditional three steps of compilation

A
  1. 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
  2. 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
  3. Code-generation
    • Taking the generated AST and turning that into machine instructions to actually carry out the operations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Since JavaScript compiles right before execution, it has to employ many tricks, like…

A

JITs, which lazy compile, and hot re-compiling

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the engine?

A

It’s responsbile for start-to-finish compilation and execution of our JS program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the compiler?

A

Handles the dirty work of parsing and code-generation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the scope?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Break down var a = 2;

A
  1. Compiler will perform lexing will be performed, to break it into tokens, which will be parsed into a tree.
  2. Encountering var a, compiler asks scope if a 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
  3. 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

In Summary

  1. Compiler declares a variable
  2. Engine looks up variable in Scope and assigns to it, if found
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe an LHS lookup

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Would a = 2; cause an LHS or RHS lookup and why?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe an RHS lookup

A
  • Stands for Right Hand Side lookup
  • Is simply a lookup of the value of some variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
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

A
  1. foo (line 5) - RHS lookup to find the value of foo, which will be a function
  2. implicit a = 2 LHS lookup when calling the foo function
  3. console - RHS lookup to find the object
  4. log - property resolution to ensure the method is on the object
  5. a (line 2) - RHS lookup to find the value of a
  6. implicit arg1 = a LHS mapping the resolved a RHS lookup’s value to arg1 parameter of the log method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe nested scope

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens when an LHS and RHS look-up fails?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly