Understand javascript--scopes and assignment operations

Source: Internet
Author: User

Scopes, as a basic function, exist in a variety of programming languages, making our programming more flexible and interesting. The underlying function is to store the values in the variables and then access and modify the values.

Maybe we all know some concepts about scopes, and some of its extensions are some of the content closures, but it might be more interesting to know where these variables are stored, and how our program accesses them.

var a = 1;

First we need to understand who is involved in our entire process when we declare variables and assign values.

1, Engine: it participates in the compilation and execution of the whole JS program.

2, compiler: It is responsible for parsing and generating the code.

3, Scope: It is responsible for the mobile phone and maintains all the identifiers are composed of a series of queries, and implemented a very strict set of rules, to determine the current execution of the code to the access rights of these identifiers.

When the engine sees var a = 1, it's not the same as what we think, and we think it's a statement, what he thinks is actually two completely different statements, one that is handled by the compiler at compile time and the other by the engine at runtime. Well, let's see how they work.

The compiler will first say var a = 1; The program breaks down into lexical units and parses the lexical unit into a tree structure, but when the compiler begins to generate code, he will handle the process differently.

Within the scope of our understanding, the compiler works by allocating memory for a variable, naming it a, and then saying that the value 1 is stored in the variable, but the fact is different.

1, when encountering Var A, the compiler first 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 continues compiling, otherwise he asks the scope to declare a variable named a in the current scope's collection.

2, and the compiler begins to generate the code required for the engine to run, and the code is used to handle the assignment of a = 1. Then the engine runs, it asks the scope, if there is a variable called a in the current scope collection, if the engine uses that variable, and if no, the engine continues to look for the variable at the ancestor scope of the current scope. Finally, if you find a, the engine assigns 1 to it, and if not found, the engine throws an exception.

So, there are two actions in the assignment of the new variable, the first one is the compiler declares the variable, and the second is the engine finds the variable in scope and assigns it a value.

Let's look at a simple code

function Demo (a) {Console.log (a)}demo (2);

Before we go on, let's take a look at LHS and RHS, as the name implies, one left and one right. This is the two queries that the engine uses for the variable, and L and R represent the left and right of an assignment (in most cases), meaning that when the variable appears to the left of the assignment, the LHS query is made, and the RHS query appears on the right. But the more precise argument is that the RHS query simply finds the value of a variable, and the LHS query finds the container itself, and assigns it. So according to this statement, we can find that RHS actually represents the "non-left". Let's look at a simple code.

var a = 1;console.log (a);

In the above code, var a = 1, a reference to A is a lhs reference, and A of Console.log (a) is actually a RHS reference. Let's look at an example:

function Demo (a) {Console.log (a); }demo (1);

The above code actually contains the RHS and LHS references. We understand the demo (1), meaning that it actually means that RHS quotes the demo value, (...) means that it needs to be executed, and in the course of execution, there is an implicit LRS reference of a = 1; This query occurs in the process of parameter passing. The Console.log (a) is also a RHS reference.

At the same time we need to know that unsuccessful RHS will cause an exception to be thrown, and an unsuccessful LHS reference will result in the automatic creation of a global variable (non-strict mode) or an exception (strict mode).

Nested Nesting of scopes

When a block or function is nested within another block or function, the scope of the nested nesting occurs. Therefore, when the variable cannot be found in the current scope, the engine will continue to look in the outer nested scope (if it has not been found to reach the global scope) until the variable is found.

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/964875/201610/964875-20161017104824857-1188301210. PNG "style=" margin:0px;padding:0px;border:none; "/>

A layer of outward formation is what we often call the scope chain, the inner layer represents the scope of the current execution environment, the outermost represents the global scope. lhs and RHS are searched at the current scope, if not found, outward, and so on, if the global scope is not found, then the discovery process will stop.


Understand javascript--scopes and assignment operations

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.