Pre-compiled function calls

I. Introduction

We talk on a pre-compiled script, this section talk about pre-compiled function calls.


Second, pre-compiled function calls

1. Process

1. Create the active object AO (Active Object)

2. precompiled:

  • Generating scope chain (scope chain)
  • Initialization arguments
  • Initialization parameter, the values ​​assigned to the arguments of the parameter
  • Find all variable declarations, in accordance with the variable name to join AO, if present, is ignored.
  • Find all function declarations, as a function of adding the name of AO, if a variable or function with the same name already exists, replace.
  • this initialization

3. Explain code execution

2. Parse

1. All variables declared in the function, the function of the pre-compilation stage is completed, all of the variables independent of the actual writing position.

function f() {
    console.log(aa); // undefined
    var aa = 5;
    console.log(aa); // 5
}
f();
复制代码

2. All function declarations in the function, the function of the stage of completion of pre-compiled, declare all variables unrelated to the actual writing position.

function f() {
    console.log(haha);
    function haha() {
        console.log(123);
    }
}
f();
复制代码

3. function, if the variables and functions of the same name, then the function will overwrite variables.

function f() {
    console.log(haha);
    var haha = 123;
    function haha() {
        console.log(456);
    }
}
复制代码

4. function, the function can only cover a variable, the function can not be overwritten.

function f() {
    console.log(haha);
    function haha() {
        console.log(123);
    }
    var haha = 456;
}
f();
复制代码

The function, the function declaration back overwrite the previous function declaration, and the parameter is ignored.

function f() {
    console.log(haha);
    function haha(a) {
        console.log('aaa');
    }
    function haha(a, b) {
        console.log('bbb');
    }
}
f();
复制代码

6. When the function is pre-compiled, you encounter a variable or function needs to access, giving priority to their own variables and functions defined in the AO, if not, it will find the upper AO defined until it reaches GO.

var scope = 'global';
function t() {
    console.log(scope); // undefined
    var scope = 'local';
    console.log(scope); // local
}
t();
console.log(scope); // global
复制代码

Third, the mind map section

Source Address: github.com/Knight174/M...

Guess you like

Origin blog.csdn.net/weixin_33763244/article/details/91399437