Precompiled and the scope chain

1.js how the code is executed?

First of all we need to know js is an interpreted language, the code is executed from top to bottom, feet down the line in order to perform, we have to know the entire code to run is divided into two stages, which is divided into two stages of pre-compiled and executed .

2. precompiled

From the name we can see, we are pre-compiled before the function began to carry out the execution.

Precompiled is, before the function execution, all variables var advance fame (or the values ​​of variables remain in place) and the overall function of the advance, saying that white is done in front of the function of

Precompiled this function is generally divided steps:

(1) Any function creates AO active object before execution

(2) then the variable we want to advance the reputation, reputation is generally ahead of fame var keyword (value at this time is undefined, except function)

(3) and then find the corresponding value entered into

(4) The last function to find fame and trying to assign this function to this variable

We give an example:

function fun(a , b , c){
 console.log(a);//function a(){}
 console.log(b);//undefined
 function a(){};
 var a=b=100;
 console.log(a);//100
 console.log(b);//100
 var b = function(){};
 console.log(b)//function(){}
 }
 fun(3);

We can come according to the above formula

There is only a case of the AO and b, and the values ​​are undefined

The third step is then performed: In this case the value of a is 3, the value of b or undefined

Then perform Step 4: At this time, the value is a function a () {}, or undefined value b

Finally, as a function of the execution result from the top

3.Active Object active objects (abstract)

    Execution Environment:

        js code execution environment is (global environment, internal environment function)

        The environment defines the data they have access to other

        Environment has an associated "active object AO"

        Environment variables and functions are so active object attributes AO

        The global environment is the outermost execution environment, active objects window object

        Code execution environment is destroyed after completion

4. Our function in the interpretation of the implementation, we must first clarify the current scope, the scope of two types, one is the global scope, the other is the local scope, in fact, the difference between these two scopes very simple

Global Scope: First, we can first find a global variable, not the global scope is determined according to a global variable, and some people may be asked how to find a global variable, the global variable is a function which, without var keyword to declare a variable, but also there is a direct function in

Reputation of the outside variables, in fact, in the window (this is only my personal understanding, or according to the actual situation)

The local scope: local scope, and it is just the opposite in the function of internal variables with the var fame (by reference)

5. There is also a global precompiled

  GO create function scope before the execution and then create AO active object is actually about the same steps with the above active object

 At this time the global pre-compiled to form a scope chain

6. What is the scope chain

Like the scope chain chains child among us in life, inter-related

Our analogy, just as we have multiple functions nested together before this function has not been executed, our global pre-compiled execution began, and this time it created a GO (GO at this time there is the outermost function body) when the function execution is now the most active objects outside AO is the second function body, precompiled second function is the first AO and GO, when the function is executed AO active objects is a second-AO the first and the GO and AO, is one such relationship, the scope chain is formed.

7. The role of the scope chain

   7.1 Variables must be "first statement after use."

        Function can be "first use, the statement" because there is a function of the process of pre-loaded (function declarations prior to the implementation of other code into the memory). Nature or function declarations in front, used after.

    7.2 Internal environment variables can be accessed variables in the external environment, not vice versa.

       Environment: Each function is an internal environment, the outermost is the global environment.

        Type: function environment, global environment

    7.3 Scope of variables is that the declaration, instead of running time

8. Priority

    Perform link can access the variable type and priority

    >>> >>> internal variables inside a function parameter >>> external variables (functions, parameters);

Guess you like

Origin www.cnblogs.com/zhangli123/p/11838356.html