On the principle of JavaScript pre-compiling

Source: Internet
Author: User

These two days again the basis of JS review, a lot of do not know or to return to the foundation, we all know that JS is an explanatory language, is to compile a line to execute a line, but before executing, the system will do some work:

1, grammatical analysis;

2, pre-compilation;

3, explain the execution.

Parsing is simple, that is, the engine will simply check your code for any low-level errors, interpretation of execution is to execute the code, the execution of the code will be pre-compiled, pre-compilation simple understanding is to open up some space in memory, some variables and functions. Let me say in detail:

Pre-compilation can be easily divided into global pre-compilation and function-Body pre-compilation, the function of pre-compilation can start from four rules;

1, create AO object; ao{}

2, find formal parameters and variable declaration, the variable and formal parameter name as the property name of AO, and assign value to undefined

3, the actual parameters and formal parameters are unified;

4, find function declaration in function, assign function body.

Remembering the above four rules, here is a simple case listing:

function Test () {
Console.log (a);
Console.log (b);
var b = 234;//Variable declaration
Console.log (b);
A = 123;//variable declaration
Console.log (a);
function A () {}//functions Declaration
var A;
b = 257;
var B = function () {}//variable declaration
Console.log (a);
Console.log (b);
function C (Y) {//Functions Declaration
var y = 1;
};
}
Test (1)
Let's see what the engine did to this code.
1, create AO object; Activation Object ao{}
2, find formal parameters and variable declaration, the variable and formal parameter name as the property name of AO, and assign value to undefined
ao{
b:undefined;
a:undefined;

}
3, the actual parameters and formal parameters are unified;
4, find function declaration in function, assign function body. So at this point a will be replaced by the function body
ao{
A:function A () {}
b:undefined
C:function C (y) {
var y = 1;
}
}
When the page is finished preprocessing, the execution begins, according to the principle of the sentence of the compilation, at which point the value of Console.log (a) is naturally: the value of function A () {},console.log (b) is: undefined.
b is then assigned a value of 234, so the value of the second Console.log (b) is: 234, and so on, the value of the second Console.log (a) is: 123,
b After a third assignment of 257, it is also assigned by function () {}, so the value of the last Console.log (b) is: function () {},console.log (a) is: 123,

Global precompilation is less than the function body pre-compilation of the third article:

1, create GO object; global OBJETCT go{}

2, find formal parameters and variable declaration, the variable and formal parameter name as the property name of AO, and assign value to undefined

4, find function declaration in function, assign function body.

The following is also explained by a simple sample:

function Test () {
Console.log (b);//undefined
if (a) {
var b = 100;
}
Console.log (b)//undefined
c = 456;
Console.log (c);//456
}
var a
Console.log (a)////undefined
Test ()
A = 10;
Console.log (a)//10
Console.log (c)//456

Define the Go object first, then the variable and the parameter as the property name, and assign the value to undefined, as follows:
go{
a:undefined;
c:undefined;
}
Then the function body is assigned in the function life, but there is no function body at this time, so no value is assigned.
The following is the execution of the statement, so the first Console.log (a) is undefined; the second is 10,c 456 (note that at this point C is not named, directly promoted to a global variable)
When executed to test (), the content inside the function test () is proactively precompiled, and an Ao object is created.

There is also an article on the pre-compilation written also more clearly: http://blog.csdn.net/q1056843325/article/details/52951114

On the principle of JavaScript pre-compiling

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.