The first part is scopes and closures

Chapter 1 scope

1. What is scope
Code looks for rule limits on variables (identifiers) during compilation and execution to determine access to the identifiers by executing code.Copy the code
2. What are scoped queries
Scope query is classified into two types: RHS query and LHS query RHS query: finds the location of the identifier and takes the value without assigning. Similar to going to the bank to withdraw money (find the bank, withdraw money from the card). LHS query: query the location of the identifier, and then perform the assignment operation. Function foo(a) {var b = a; function foo(a) {var b = a; return a + b; } var c = foo(3) there are 3 LHS queries and 4 RHS queries in the above code.Copy the code
3. What is a scope chain
A scope chain is formed when a block level or function scope is nested within another block level or function scope. The engine looks for variables from when's execution scope, and if it can't find them, it looks up one level. When the outermost global scope is reached, the query process stops whether it is found or not. RHS queries and LHS queries follow this procedure when looking for variables.Copy the code
4, abnormal
In non-strict mode: if the LHS query does not find the variable in the global scope, the variable will be created and declared in the global scope. RHS queries that do not find the variable in the global scope raise ReferenceError. In strict mode: if the LHS query does not find the variable in the global scope, it will raise referenceError and will not create the variable implicitly. RHS queries that do not find the variable in the global scope raise ReferenceError.Copy the code
ReferenceError is an error caused by the failure of the scope to find the variable.
TypeError is an error caused by illegal use of a variable found in scope.