"Pre-compilation phase" and "execution phase" _ JavaScript skills during javascript running

Source: Internet
Author: User
This article mainly introduces information about the "pre-compilation phase" and "execution phase" during JavaScript running, if you need javascript, you can refer to it. javascript is a weak language compared to other languages. In other languages such as java, program execution requires a compilation stage, in javascript, there is also a similar "pre-compilation stage" (the pre-Compilation of javascript is based on the code block as the scope of "script" and "script", that is, every time a code block is encountered, it will be pre-compiled> executed). Understanding the execution mechanism of the javascript engine will help to summarize the ideas in the js code writing process.

First, the two declaration methods in javascript are var and function. The former declares variables, and the latter declares methods.

During pre-compilation, javascript has made two solutions for these two statements.

Script var a = "1"; // declare the variable a function B () {// declare Method B alert ();} var c = function () {// declare the variable c alert ();} script

In the code block above, a and c assign values to variables, and B are function declarations. When the above code is executed, the pre-compilation phase is started, assigning values to variables a and c will open up a memory space in the memory and point to the variable name, and assign values to undefined

For function declaration, the memory space will be opened up, but the assigned object will assign the declared function to the function name.

Pre-compilation phase: (PS: regardless of the order in which variables are declared and functions are declared in the code, variables are declared before the function is declared in the pre-compilation phase)

《script》    var a = undefined;    var c = undefined;         var b = function(){      alert();    } 《script》 

Execution phase:

《script》    a = "1";    c = function(){      alert();    }《script》 

Overall steps:

《script》    var a = undefined;    var c = undefined;        var b = function(){      alert();    }    a = "1";    c = function(){      alert();    }《script》 

Question:

《script》    var a = "1";    function b(){      alert(a);      var a = "2";    }    b();《script》 

Ps: javascript pre-Compilation

1. predefine variables before predefine Functions

2. Variable pre-compilation is declared only, and Initialization is not performed.

Iii. Functions Defined by function statements not only declare the function name, but also process the function body.

Iv. Anonymous functions are not pre-compiled

Function f () {// declare the function f return 1;} alert (f (); // returns 1 var f = function () {// defines the anonymous function f return 2;} alert (f (); // returns 2

Predefines the variable f first, and then the function f () with the same name overwrites the variable f, so the first output of 1; variable pre-Compilation

Var f = function () {// defines the anonymous function f return 1;} alert (f (); // returns 1 function f () {// declare the function f return 2;} alert (f (); // return 1

Predefines the variable f, and then the function f () with the same name overwrites the variable f.

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.