Advanced Course -2 javascript

Scope var role lexical analysis

js Scope

    var A = 12 is ;
     function A1 () {
         var A =. 11 ;
         function B () {
             function C () { 
                the console.log (A); 
            } 
            C (); 
        } 
        B (); 
    } 
    A1 (); 
    / * Variable how to find a 
    first look inside the function 
    to find less than the outer function is to seek 
    the guidance of the global area 
    * /

var usefulness

var A = 12 is ; 
    b =. 1 ;
     function Test () {
         var C = 12 is; // local variable 
        b = 2; // without var outwardly find find the global variable b and assigning 
    } 
    Test (); 
    the console.log ( b); // prints global variables

lexical analysis:

1, the first analysis parameters

2, reanalysis variable declaration

3, analysis function declaration

Specific steps:

1, the moment before the function is run, generating active object referred to as AO

2, the function declares, AO forming property value is the value of the argument, the argument was not pass undefined

3, analysis of variable declarations: var age as 

    If the property has not been age AO, AO is added attribute value is undefined

    If you already have age properties not make any impact on AO

4, the analysis function declaration, such as function foo () {};

     Put the property with assigned to AO.foo

     If the property already exists relentless coverage

 

var A = 12 is ; 
    B =. 1 ;
     function Test (C) {
         / * lexical analysis: 
        parameter analysis: 
        test.AO.c = 12 is; 
        declare variables analyzed 
        test.AO.c = undefined 
        parameters if the previous analysis already shaped this property is not affected 
        or assignment 
        function declaration skip 
        * / 
        var c =. 1; // local variables 
        the console.log (c); // execution is assigned to c 12 is 
    } 
    Test (); 


    function test2 (G) { 
        Console .log (G); 
        function G () { 
            Alert ( 2 ); 
        }  
    }
    test2 ( 12 is);
     / * Note part analysis function declaration overwrite properties on arguments g where AO While not perform the same test, but this force is overwritten when re lexical analysis * / 

    function Test3 (g) { 
        the console.log ( g); 
        var g = function () { // At this time, the lexical analysis of g as a variable rather than a function declaration 
            Alert (2 ); 
        } 
    } 
    Test3 ( 12 is);

Look at an example:

 

T (); 
    t2 (); 
    function T () { 
        Alert ( 'T' ); 
    } 
    var t2 = function () { // lexical analysis process t2 of expression statement is not executed undefended 
        Alert ( 't2' ) ; 
    }

 Complex example:

    function Test (A) { 
        Alert (A); 
        function A () { 
            Alert (A); 
        } 
        A (); 
    } 
    Test ( 12 is ); 

    / * 
    Analysis outer Test 
    test.AO = {} 
    analysis parameters 
    test.AO. a = 12 
    analysis declare variables 
    analysis declare function 
    test.AO.a = function () { 
            Alert (a); 
        } 
    performed test (12) 
    the output function 
    analysis a function 
    analysis parameter 
    analysis declare variables 
    analysis declare function 
    performs a () 
    can not be found the outer layer to find a test.AO 
    output functions 



    * /
function test1 (A) { 
        Alert (A1); 
        var A1 = function () { 
            Alert (A1); 
        } 
        A1 (); 
    } 
    test1 ( 12 is );
     / * 
    lexical analysis 
    test1 
    test1.AO = {} 
    test1.AO.a 12 is = 
    test1.AO.a1 = undefined 
    performed test1 
    pop undefined 
    and a1 function assigned function () { 
            Alert (a1); 
        } 
    lexical analysis test1 () 
    performs test2 
    a1 to find the outer 
    eject function 

    * /

 

Guess you like

Origin www.cnblogs.com/webcyh/p/11387941.html