Source:https://github.com/getify/you-dont-know-js/blob/master/scope%20&%20closures/ch1.md
Common compliation Steps
- tokenizing/lexing: breaking up a string of characters into meaningful (to the language) chunks, called toke Ns. For instance, consider the program:
var a = 2;
. This program would likely is broken up into the following tokens: var
, a
, =
, 2
, and ;
. Whitespace may or May is persisted as a token, depending on whether it's meaningful or not.
note: the difference between tokenizing and lexing are subtle and academic, but it centers on whe Ther or not these tokens is identified in a stateless or stateful way. Put simply, if the tokenizer were to invoke stateful parsing rules to figure out whether a
should b E considered a distinct token or just part of the another token, that would be Lexing.
- Parsing:taking a Stream (array) of tokens and turning it into a tree of nested elements, which collectively represent the Grammatical structure of the program. This tree was called an "AST" (Abstract Syntax Tree). The tree for
var a = 2;
might start with a top-level node called VariableDeclaration
, with a child node called Identifier
(whose value is a
), And another child called AssignmentExpression
which itself have a child called NumericLiteral
(whose value is 2
).
- Code-generation:the process of taking an AST and turning it into executable code. This part varies greatly depending on the language, the platform it ' s targeting, etc.
Complier Model in JS
- Engine: Responsible for Start-to-finish compilation and execution in our JavaScript program. Compiler : One of Engine ' s friends; handles all the dirty work of parsing and code-generation (see previous section).
- Scope: Another friend of Engine; Collects and maintains a look-up list of all the declared identifiers (variables), and enforces a strict set of rules as T o how these is accessible to currently executing code.
LHS & RHS
- An RHS look-up are indistinguishable, for we purposes, from simply a look-up of the value of some variable. (Get the value of)
- An LHS look-up are trying to find the variable container itself, so that it can assign.
- In this, RHS doesn ' t really mean "right-hand side of a assignment" per se, it just, more accurately, means " Not left-hand side".
function Foo (a) { // RHS of console Object, LHS of parameter in log/ RH S of foo// inplied a = 2//LHS of a
function Foo (a) { var// LHS of B, RHS of a return// RHS of A, RH S of B}var// LHS of C, RHS of foo// implied a = 2//LHS of a
Nested Scope
function Foo (a) { + b);} var b = 2// 4
The RHS reference b
for cannot is resolved inside the function foo
, but it can is resolved in the Scope s Urrounding it (in this case, the global).
Error
function Foo (a) { // RHS can not find B in the scopes, results in a referenceerror being thrown by The Engine b =2);
If the Engine is performing a LHS look-up, and it arrives at the top floor (global Scope) without Findi Ng it, if the program isn't running in "Strict Mode" [^note-strictmode] and then the global Scope would create a new Variable of that name in the global scope, and hand it back to Engine.
Compiler theory in JavaScript