JavaScript precompiled

    The execution order in js (JavaScript) is quite a headache. Many friends only remember two sentences about precompilation: all variable declarations are improved as a whole, and all function declarations are improved as a whole. It is true that just remembering these two sentences can solve some, but it is a little shallow. If you want to solve the following problem

    a(10)

    function a(a){   

        a = 1;        

        console.log(a)

        there is a

        console.log(a)

    }

    console.log(a)

    var a = 1;

    Bewildered. If you only rely on those two words, you can't go anywhere in the world. No ink, go directly to the topic

    Let’s talk about formal parameters and actual parameters here. The formal parameters are the variables defined in the () after the function declaration, and the actual parameters are the values ​​that are actually passed when the function is called.

    function student(name, age){//name, age are formal parameters

        

    }

    student("snail", "300")//It belongs to the actual parameter

    Precompiled:

        At the moment before the function (function) is executed, it will first create a context during the execution, that is, the pre-compilation link, there are four steps

            Step 1: Create an AO object (Active Object)

            Step 2: All variable declarations in the function body are promoted as a whole, and the value is undefined (note: formal parameters also belong to variable declarations)

            The third step: make the formal parameter and the actual parameter value correspond, that is, assign the value of the actual parameter to the sexual parameter

            Step 4: All function declarations (not expressions!) are promoted as a whole, and the value is the function body. If the variable and function have the same name, the function will overwrite the variable's value

        This is the pre-compilation link of the function function. In fact, the entire js file can also be regarded as a function, and the script tag is regarded as the function keyword, so the global also has its own pre-compilation link.

            Step 1: Create a GO object (Gloabl Object global object)

            Step 2: All variable declarations in the function body are promoted as a whole, and the value is undefined

            Step 4: All function declarations (not expressions!) are promoted as a whole, and the value is the function body. If the variable and function have the same name, the function will overwrite the variable's value

        This is the global precompile link.

        When a function is executed, an execution context is generated. An execution context corresponds to the environment of a function during execution. Each execution context is unique. After the function execution ends, the execution context It will be destroyed, and the same function will be called multiple times, which will generate multiple execution contexts.

    The above point must be understood and remembered, otherwise subsequent scopes, scope chains, and closures will be affected.

    Looking back at the previous question now, is it so easy? If there is something you don't understand or think there is something wrong with what I wrote, please comment below.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326083930&siteId=291194637