Pre-compilation of JavaScript

Source: Internet
Author: User

JavaScript is an interpreted weakly typed language, and when executed in a browser, the browser previews a piece of code for parsing, checks the correctness of the syntax, and then compiles it to the last sentence, which is simply represented as a three-step process: Parsing---pre-compilation--Interpreting execution;

Insert a section of code first:

Before we understand precompilation, we need to first understand function declarations and variable assignments: first a line of code to explain what a function declaration is,

People who believe that there are some JavaScript basics should be able to easily understand this line of code, which simply declares a common function, and the variable is assigned the following value:

In the development of the project, we often define variables to accept the data passed, for example, when sending an AJAX request, the backend will return some data to us, we will define some variables or arrays to accept the data, and then bind the data to the DOM element;

After understanding what the function declaration and variable assignments are, we begin to explain the precompilation process.

The browser will pre-compile the script code block before executing it, and the precompilation will generally be divided into the following four steps:

1. Create AO (Activation object: Execution period context) objects;

2. Find the formal parameter and the variable declaration, the variable and the parameter name as the AO attribute name, the value is undefined;

3. Unify the parameter values and the formal parameters;

4. In the function body to find the function declaration, the value assigned to the function body;

Finally, the browser begins to interpret a line of code that executes a line of code, and if the variable is re-assigned in the code block, its final value is the value that the block is re-assigned to, such as the first Console.log (a) in the example above, the value of function A () {}, At the next step the variable A is re-assigned, var a = 123; So in the second third Console.log (a), the output value of a is 123, and then the function executes to var B = function () {}, and the following starts execution of Console.log (b), So the value of B is function () {};

Finally, after understanding the precompilation, we can naturally draw the result of this example:

For the pre-compilation understanding, we need to do some more examples to deepen the impression, is the so-called practice makes perfect!

Pre-compilation of JavaScript

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.