Understanding of JavaScript variable elevation

Source: Internet
Author: User

Nonsense not to say, directly on the code (this is an example of what is seen in the JavaScript Face Object Programming Guide)

var a=123; function f () {  alert (a);   var a=1;  alert (a);          } f ();

The explanation in the book is this: when JavaScript executes into a new function, all variables declared within the function are moved to the beginning of the function. This phenomenon is called Ascension. and only the declaration of the variable is promoted.

The above example can be equivalent to:

var a=123;
function f () {var A;
alert (a);
A=1;
alert (a);
}
f (); The answer is obviously 1.

The book just considers it as a definition, telling the reader to remember. In fact, we can start from the JavaScript compiler principle, figuring out how this code is compiled is not rote!!

So let's start by simply talking about how the JavaScript code is compiled and executed (plain talk) first the JavaScript code is compiled and executed primarily by the JavaScript engine, the compiler, and the scope of three.

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

Compiler: Responsible for parsing and code generation;

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

Perhaps not very clear, we first look at the traditional compiler compiler process, a piece of code execution will need to go through the following three procedures (its actual process is certainly more complex than this):

1, lexical analysis: A string consisting of characters is decomposed into meaningful blocks of code. These blocks of code are called lexical units. For example: Var a=2; This code will be decomposed into var,a,=,2, the lexical units.

2, Syntax analysis: the lexical unit is converted into a hierarchical nesting of elements of the "abstract syntax tree" representing the structure of the program syntax;

3, code generation: The process of converting an "abstract syntax tree" into executable code. This process is related to the language and target platform.

Since JavaScript differs from other languages that have a dedicated compiler such as C (such as HELLO.C is compiled into an executable hello.0 program, here the stage declaration assignment has already been completed and can be executed if the compilation succeeds.) While the JavaScript code compilation phase is just a declaration phase of a variable, its assignment is performed when the code is actually executed. This is why the JavaScript code must be executed after it has been written to know that there are no errors.

Well, the concept of the introduction of the basic almost, the above code on how to use the compiler to understand it! Let's start with a simple understanding of Var a=2; this code. First this code is given to the compiler: the compiler is responsible for parsing, it will first declare Var a, and then go to its scope before the declaration of a, if it has been declared to ignore the declaration, if not found in the scope of the declaration of a new variable A, The compiler then compiles the code into code that the engine can run, which is used to handle a=2, and the engine will first look for a variable in the scope after it gets the code, and if it does, it will throw an error if it does not. This process concludes that the code first declares the relevant variables in the compiler and then hands them to the engine, and the engine operates on those variables (assignment, etc.).

With the above analysis process, let's look at the first code: The reason why the book can be changed into the following code, is because this code will be compiled in the compiler, the process of compiling the code in the scope of the relevant variables to declare, which is to work before the engine code execution, so rewrite the time Var A will refer to the front of the scope, and then the JavaScript engine will execute the code further, such as assigning values to variables, after compilation is complete. The process is that the word "ascension" is mentioned in the book . Some of the other details are not to be discussed. The second piece of code should be clearly understood. (Note: Ascension can only be promoted within the scope of the variable itself, not across the scope to the outside.) )。

Give an example of an exercise:

f (); Hello world!
var F;
function f () {
Aler ("Hello world!");
}
F=function () {
Alert ("Hello javascript!");
}

Rewrite the form I will not write, I can write it completely.

  

Understanding of JavaScript variable elevation

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.