You don't know the JavaScript learning note about the scope

Source: Internet
Author: User

Three elements of the handler:

engine : The process of compiling and executing.

compilers : Parsing and code generation, and so on.

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

Example:

var a=2; The parsing process:

1. The compiler asks if the scope already has a variable of that name

IS: Ignore the statement and continue execution;

No: Declare a new variable, named A;

2. Compiler generates code for engine

The engine runs first asking for scope, whether there is a variable in the current scope collection:

Yes: The engine will use this variable;

No: The engine continues to look for the variable;

Found: Assign 2 to a.

Not found: throws an exception.

Summarize:

A variable assignment performs two actions:

1. The compiler declares a variable;

2. The runtime engine queries (LHS queries) This variable in this scope, and can be found on the assignment.

Engine Query Method:

The "L" and "R" codes are left and right respectively. What's left and right? Is the left and right side of an assignment operation.

The LHS query occurs when the variable appears to the left of the assignment operation, and the RHS query is executed when it appears on the right.

LHS query:

1. Who is the target of the assignment operation;

2. The purpose of the lookup is to assign a value to the variable (= operator or an operation that passes in the parameter when the function is called).

RHS query:

1. Who is the source of the assignment operation;

2. The purpose of the lookup is to get the value of the variable.

Type of error:

Referenceerror Exception:

1. Unsuccessful LHS in strict mode (unsuccessful LHS in non-strict mode causes the auto-hide to create a global variable without reporting an exception);

2. Unsuccessful RHS.

TypeError Exception:

If the RHS query finds a variable, but you try to make an unreasonable operation on the value of the variable, such as trying to make a function call to a value of a non-function type, or a property in a value that references a null or undefined type, Then the engine throws another type of exception.

Scope Nesting Rules:

The engine looks for the variable from the current execution scope and, if it cannot find it, continues to look up at the top level.

You don't know the JavaScript learning note about the scope

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.