"You don't know JavaScript" reading notes (a) scope

Source: Internet
Author: User

Noun

Engine: Responsible for compiling and executing the entire JavaScript program from beginning to end.

Compiler: Responsible for parsing and code generation .

Scope: Collects and maintains a series of queries consisting of all declared identifiers (variables), and implements a very strict set of rules that determine the access rights of the currently executing code to these identifiers.

LHS: The left side of the assignment operation (which is understood as the target of the assignment operation, the LHS query attempts to find the container itself of the variable and assigns it a value ).

RHS: The right side of the assignment operation (understood as who is the source of the assignment operation, the RHS query is the value of a variable).

Compilation of JavaScript

Javascript is a compiler language (note ①).

Unlike a traditional compiled language, it is not compiled in advance, but is compiled in a few subtle and even shorter times before the code executes.

Compilers and scopes

For example var a = 2;

The JavaScript engine thinks there are two completely different declarations, one handled by the compiler at compile time and the other by the engine at run time.

The compiler first decomposes the program into lexical units, which is called 1. Word breaker/lexical analysis (tokenizing/lexing), and then parse the lexical unit into a tree structure (abstract Syntax tree,ast, abstraction syntax tree), the process is called 2. Analytic/syntactic analysis (parsing). When you encounter Var A, the compiler asks the scope if a variable with that name already exists with a collection of the same scope, and if so, the compiler ignores the declaration (ignoring var a), otherwise it will require the scope to declare a new variable in the current scope's collection and name it A;

Next the compiler will be engine 3. generate The code that is required by the runtime to handle the assignment of a = 2.

When the engine runs , it first asks the scope whether there is a variable called a in the current scope collection. If it is, the engine will use this variable, and if no, the engine will continue to look for the variable.

If the engine eventually finds a variable, it assigns 2 to it, or the engine throws an exception.

Analysis when the engine runs code

When the engine runs the code, it looks for the variable A to determine if it has been declared, and the lookup process is assisted by the scope.

For example Console.log (a);

Where a reference to a is a RHS reference, because here a does not give any value, but need to find and get the value of a, to pass to Console.log (...).

Again as a = 2;

Here the reference to a is a LHS reference, to find a target for the assignment operation = 2.

Cases

1 function Foo (a) {2    Console.log (a); 3 }4 foo (2);

Print out 2.

Analysis:

1. Line 4: (engine in scope) for Foo for RHS reference;

2. Line 1: (Engine in scope) LHS a reference to a (implicitly assigning values to parameter a);

3. Line 2: (engine in scope) RHS reference to the console and check the resulting value for the log method;

4. Line 2: (engine in scope) RHS a reference to a.

Cases

1 function Foo (a) {2     var b = A; 3     return A +b; 4 }5var c = foo (2);

The function returns 4.

Analysis:

1. Line 5: (engine in scope) for C LHS Reference (c = ... );

2. Line 5: (engine in scope) for Foo (...) for RHS reference (foo (2 ...);

3. Line 1: (Engine in scope) for a LHS reference (a = 2);

4. Line 2: (engine in scope) for B for LHS reference (b = ... );

5. Line 2: (engine in scope) for a RHS reference (= a);

6. Line 3: (engine in scope) for a RHS reference (a ... );

7. Line 4: (engine in scope) is a RHS reference (... b) for B.

Scope nesting

When a block or function is nested within another block or function, the scope of the nested nesting occurs. Therefore, when a variable cannot be found in the current scope, the engine continues to look in the outer nested scope until the variable is found, or the outermost scope (global scope) is reached.

Cases

1 function Foo (a) {2     console.log (A + b); 3 }4var b = 2; 5 foo (2);

Print out 4.

Analysis:

1. Line 4: (Engine in the global scope) to B for LHS reference;

2. Line 1: (Engine in Foo scope) a LHS reference to A;

3. Line 2: (Engine in Foo scope) a RHS reference to A;

4. Line2: (Engine in foo scope) RHS reference to B, but the B variable is not found within this scope

5. The engine finds the B variable within the global scope and RHS a reference to B.

Abnormal

Cases

1 function Foo (a) {2     console.log (A + b); 3     b = A; 4 }5 foo (2);

Results:

Engine throws Exception:referenceerror

Analysis: The engine throws a Referenceerror exception when the variable B cannot be found in any scope, that is, when a RHS reference to B is not found.

In addition, if the engine cannot find the target variable within any scope when executing the LHS query, there are two things: 1. The program runs under "non-strict Mode", the global scope creates a variable with that name and returns it to the engine, and if it is run in strict mode, it will also throw Referenceerror exception.

Cases

Non-strict mode:

function Foo (a) {    = A;    Console.log (b);} Foo (2);

Print out 2:

Strict mode:

"Use strict"; function Foo (a) {    = A;    Console.log (b);} Foo (2);

If RHS queries a variable, but attempts to perform an unreasonable operation on the value of the variable, such as attempting to make a function call to a value of a non-function type, or referencing a property in a value of null or undefined type, the engine throws an TypeError exception.

= Null;console.log (b.test);

Note:

① the definition of Javascript from other websites

Mozilla.org:JavaScript is a lightweight, interpreted, object-oriented scripting language ... It is a prototype-based, multi-paradigm dynamic scripting language.

Baidu Encyclopedia: JavaScript a literal translation script language, is a dynamic type, weak type, prototype-based language, built-in support type. Its interpreter is called the JavaScript engine and is part of the browser.

Reference: Nanyi "Javascript Strict mode detailed"

More examples: http://www.cnblogs.com/dee0912/p/4396356.html

"You don't know JavaScript" reading notes (a) scope

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.