JavaScript scoping issues: pre-analytical, analytical global and local scope, the scope chain

To understand the scope JS problem, we must first understand how JS parser browser when the browser reads script script code, JS parser began to work. Its working step is divided into two parts: 

JS parser: 

1. "find something" (pre-parsed): var function parameters 

Example:

 

   alert(a); //undefined

   There are a = 1;

   alert(a); //1

   function fn1(){alert(2);}

 

JS parser will first find var function parameters are pre-parsed. 

When found var defined variables a, resolved to a = undefined, does not read the specific value. In other words, all the variables before the official run, ahead of all assigned a value - undefined. 

When read funtion, resolved to: fn1 = function fn1 () {alert (2);}. All functions, before the official running the code, is the entire function block 

2. Interpretation of the code line by line: 

JS JS parser formal reading the code, will first read the pre-parsing result obtained, so this pop First Regular undefined, when formally reads a = 1, it will modify the value obtained when the pre-resolved, then a pop-up on the value of 1.

 

 Example:

1

       alert (a); // given

       a=1;

1

Note: Because there is no var a statement, the value is less than a pre-parsed, and can not find a formal reading the code, the browser will complain.

 

Here are some specific examples will gradually deepen understanding of the scope: 

Example:

 

alert(a); //function a(){ alert(4);}

There are a = 1;

alert(a); //1

function a(){ alert(2);}

alert(a); //1

There are a = 3;

alert(a); //3

function a(){ alert(4);}

alert(a); //3

a (); // error, the parser is to save the last 3

 

analysis:

 

Pre-resolving process:

        a=...,

        a=function a(){ alert(2);}

        a=...,

        a=function a(){ alert(4);}

Note: The browser first pre parsing, a pre-analysis results as shown above. When the browser during the pre-parsing encountered variables and functions of the same name, will leave the function; the face of multiple functions of the same name, will leave after the function declaration. When the official read the code, it will modify saved when the pre-analytical value, keep in mind that the expression will change the value of the pre-analytic, but the function declaration does not modify the value of the pre-parsed.

 

Example:

 

There are a = 1;

function fn1(){

    alert(a);        

    There is 2;

}

fn1(); //undefined

alert(a); //1

 

analysis:

 

     Preresolution: a = undefined;

                fn1=function fn1(){

                      alert(a);

                       There is 2;

                 }

    Progressive read the code: a = 1;

                 Preresolution: a = undefined;

                 Progressive read the code: a = 2;

This pre-parsed regular session twice, as indicated above, the variable defined within a function only work within a function, the final value of a pop-1.

 

Example:

 

There are a = 1;

function fn1(){

       alert(a);                 

       a=2;

}

fn1(); //1

alert(a); //2

 

Analysis: a function is not used here in the var declared, could not find any information of a pre-analytic function, it will look for a jump out of scope, the scope chain which is referred to, when a = 1.

 

Example:

 

There are a = 1;

function fn1(a){

       alert(a);

       a=2;

}

fn1(); //undefined

alert(a); //1

 

Analysis: When a function parameters corresponds var a = ..., so a = function defined preresolved

 

          1. Pre-analysis:

                 a=...,

                  function fn1(a){

                        alert(a);

                        a=2;

                  }

          2. Reading the code line by line:

                 Global a = 1;

                 Function calls: a pre-parsing = ...; progressive read the code a = 2, but the function of external global variable a constant value

Example:

 

There are a = 1;

function fn1(a){

       alert(a);     

       a=2;

}

fn1 (a); // 1: This parameter is derived from the overall amount a = 1;

alert(a); //1

 

NOTE: during a function, the function parameters will start to start reading the code, the parameters are a value of 1.

Guess you like

Origin www.cnblogs.com/tongguilin/p/12230156.html