JS Code Execution mechanism

Source: Internet
Author: User

JS code from compile to execution

We write a JS code, JS engine is not in the order we write from top to bottom in order to compile and execute, first of all, according to their own rules of our code compiled first, and then from the top to the bottom of the code to execute the compilation.

In the global scope, JS first declares our function, then the variable promotion mechanism that we often hear, then compiles it in the order in which we write the code, and then executes the compiled code.

Look at the following code:

function fn () {    console.log (a);}
FN ();
var a = "value";

A very common face question the FN function executes the output undefined, and we follow the above-mentioned JS compiler execution mechanism to simulate the compiled code:

function fn () {    console.log (a);} var a == "value";

Then we execute our code from top to bottom in the compiled order, and we can see that when the FN function executes, A is not assigned, and a is undefined.

In the function scope, the compilation order is: first declare the existence of the function, then the internal function, then the variable, and then in order to compile the code we write, if there is the following code:

function Wrap (A, b    ) {var c = "C";     var str = a+b+c;     function Pirint () {        console.log (str)    }    print ();} Wrap ("A", "B");

A very standard piece of code, the Wrap function internally simulates the compiled code as:

var a == "a"; var b == "B"; function _print () {    console.log (str);} var c = undefined; var str == "C"= a+b+c;_print ();

Figuring out the execution mechanism of JS may not allow me to write more powerful code, but can let us write more elegant code, followed by a few pen questions can also let us more handy, in the high wall is a block of bricks a grain of sand base, Code is also the same in complex code is based on the basic code as the foundation.

JS Basic use

No matter how the code is written, the basic knowledge is unchanged.

1. "." The left side of the operator is always an object, and the right side is the property (or the property value may be an object), and the result is the property value of the property.

2. "()" is always a call to a method.

3. "()" is always a parameter, if the argument inside is an expression, the expression is executed first, and then the result of the expression executes as an argument.

For example, we often use the chained operation of JQ: $ ("#demo"). html (). CSS (), $, HTML, CSS are a method name, and each method returns an object, which allows us to implement chained operations that are convenient for our development.

JS Code Execution mechanism

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.