"Reading notes" You don't know JavaScript (roll up)--what is the scope

Source: Internet
Author: User

Chapter I. Scope

1. Understanding the Scope

Introduction to several nouns

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

Compiler: Responsible for parsing and code generator and other dirty live dirty

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.

for var a = 2, decompose to see how the engine and so on work.

The compiler first decomposes the program into lexical units and then parses the lexical unit into a tree structure. However, when the compiler begins to generate code, it will treat the program differently than expected.

The compiler will handle the following

1) encountering Var A, the compiler asks if the scope already has a variable of that name that exists in the same scope's collection. If so, the compiler ignores the declaration and proceeds to compile by 2, otherwise it will require the scope to declare a new variable in the current scope's collection and name it a.

2) The compiler will then generate the code required for the engine to run, which is used 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 so, the engine will use this variable, and if not, the engine will continue to look for the variable. If the engine eventually finds a variable, it will assign 2 to it. Otherwise, the engine throws an exception.

Summary: The assignment of a variable performs two actions, first the compiler compiles the current scope to declare a variable (if it was not previously declared), and then at run time the engine looks for the variable in the scope, and assigns a value if it can be found.

2. Compiler LHS and RHS

The variable appears on the left side of the assignment operation for the LHS query, which appears on the right when the RHS query

Consider the following code:

Console.log (a);

Where a reference to a is a RHS reference (because here is the value of a is passed to Console.log (..) )。

In contrast, for example:

A = 2;

The reference to a here is the LHS reference, which assigns a value of 2 to a.

Summary: The meaning of LHS and RHS is "left or right of the assignment operation" does not necessarily mean "= left or right of the assignment operator". There are several other forms of the assignment operation, so it is conceptually best to interpret it as "who is the target of the assignment operation (LHS)" and "who is the source of the assignment operation (RHS)".

Consider the following procedure, which has both LHS and RHS references:

function foo () {    //2   }foo (2);

The last line of the Foo (..) function calls for a RHS reference to Foo, meaning "to find the value of Foo and give it to me." and (..) It means that the value of Foo needs to be executed, so it's better to really be a value of a function type!

The implicit a = 2 operation in the code can be easily ignored, and a LHS query is made here.

Small test:

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

1), find all the LHS query

c =:; a = 2 (implicit variable assignment), B =:

2), find all the RHS query

Foo (2.., =a;, a.. 、.. B

3. Scope nesting

When a block or function is nested within another block or function, the nested nesting of the scopes 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 (that is, the global scope) is reached.

4. Abnormal

The behavior of the LHS and RHS two queries is not the same in cases where the variable is not declared (it cannot be found in any scope).

Consider the following code:

function Foo (a) {    + b);     = A;} Foo (2);

The variable cannot be found the first time a RHS query is made on B. In other words, this is an "undeclared" variable because it cannot be found in any of the relevant scopes.

If the RHS query does not find the required variable in all nested scopes, the engine throws a Referenceerror exception. It is worth noting that Referenceerror is a very important exception type.

In contrast, when the engine executes a LHS query, if the target variable is not found in the top level (global scope), a variable with that name is created in the global scope and returned to the engine, provided that the program is running in a non-strict mode.

Next, if the RHS query finds a variable, but you try to make an unreasonable operation on the variable, such as trying 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 another type exception, It's called TypeError.

Referenceerror is related to the discriminant failure of the same scope, while the TypeError represents the success of the scope, but the result is illegal or unreasonable.

"Reading notes" You don't know JavaScript (roll up)--what is the 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.