Chapter 1 scope

The ability to store a value in a variable and access or modify that value later.

1. Compilation principle

Participle —————> parse —————> code generation

A. Tokenizing/Lexing To decompose a string composed of strings into meaningful code blocks called lexical units (tokens). Whether Spaces are used as this method unit depends on whether empty space makes sense in the language. The main difference between word segmentation and lexical analysis is that lexical units are identified in stateless and stateless ways. Simply put, if a lexical unit generator calls a stateful parsing rule to determine whether A is an independent lexical unit or part of another lexical unit, the process is called lexical analysis.

Parsing transforms a stream of lexical units (arrays) into a Tree in which all components are nested hierarchally, a process known as the Abstract Syntax Tree (AST).

C. Code Generation The process of converting an AST into executable code is called code generation. This process depends on language, target platform, and so on.

Engine, compiler, scope

The assignment of a variable performs two actions: first, the compiler declares a variable in the current scope (if it has not been declared before), and then the runtime engine looks for the variable in the scope and assigns it if it can find it.

LHS RHS

The LHS query view finds the variable’s container itself. The RHS query simply looks up the value of a variable

Nesting of scopes occurs when a block or function is nested within another block or function. Therefore, if a variable is not found in the current scope, the engine will continue searching in the outer scope until it finds the variable. Or tick to the outermost scope (that is, the global scope).

A ~ scope is a set of rules for determining where and how to look for variables (identifiers). If the purpose of the lookup is to assign a value to a variable, an LHS query is used; If the goal is to get the value of a variable, RHS queries are used. ~


Chapter 2 lexical scope

There are two main working models of scope. The first is the most common lexical scope adopted by most languages, and the other is called dynamic scope.

JavaScript uses lexical scope.

1. Lexical stage

Regardless of the shadowing effect, scoping searches always start at the innermost scope of the runtime, and the host works outward or upward until the first matching identifier is anticipated.

Global variables automatically become properties of the global object, so they can be accessed indirectly by referring to properties of the global object rather than directly by the name of the global object. This technique allows access to global variables that are masked by the same name. But non-global variables that are obscured cannot be accessed anyway.

2. Cheat morphology

How do I change the lexical scope at run time

eval() setTimeout() setInterval() new Function()

Avoid using

The above two modify or create new scopes at run time to trick other lexical scopes defined at write time.