Examples of pre-compilation in JS

Source: Internet
Author: User

JS, as a scripting language, can run without being compiled, but it is especially necessary to encounter pre-compilation problems, especially when variables or functions have the same name. In order to better understand the operation mechanism behind the JS language. The author uses an instanced approach to explain the precompilation I understand.

Understanding precompilation begins by clarifying two concepts: function declarations and variable assignments.

function Ledi () {}//functions Declaration

This form of writing is a function declaration, that is, declaring a function, the script will be pre-compiled before execution.

var ledi= function () {}//variable assignment


This is a variable assignment, the function in the JS language is also a data, the anonymous function as a variable assignment to the defined variables. This form will only allocate a memory space to the variable Ledi during the precompilation process, and will not initialize. The initialization process executes at execution time.

Well, figure out the above concept, on the example. Example Reference ( click here ). The original author only explained the first two examples in detail, and the author seems that the next two are also very important, then the author will be screened out representative to explain.

Example code one:

This code, first of all, to determine that two are function declarations. will be processed in the pre-compilation phase, and the function name is the same, the function will be defined as defined later, in the execution phase only see the function results defined later, it is not difficult to understand.

Example code two:

This code, first of all, to determine the two variables are assigned to the value. In the pre-compilation phase, the two variable names allocate a memory space to hold the variable Ledi content. When the code executes, it executes and assigns the values sequentially, and the results are given in two successive ways.

Example code three:

This code, first of all, to determine that the former is a function declaration, the latter is a variable assignment. After the precompilation is finished, the second line is executed first, actually the contents of the first row, and the third line is the equivalent of the Ledi variable being re-assigned, which is initialized, and the fourth row executes the function of the third row after the re-assignment.

Example code four:

Window.alert (Ledi); function Ledi () {}//The four lines of code execute Window.alert (Ledi); var ledi = 123;

The above code is executed separately, verifiable, function precompilation executes, variable assignment is allocating space first, and then assigning value when executing.

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.