Study notes about JavaScript pre-compilation

1. Precompiled Prelude

1.imply global implies a global variable: that is, any variable that is assigned a global value if it is assigned without a declaration.

<script>
    a=123;
    console.log(a);                  //123
    console.log(window.a)     //123    
</script>


2. All declared global variables are all properties of window

<script>
    var a=123;
    console.log(a);                         //123
    console.log(window.a)            //123    
</script>


3. Function declaration, overall promotion (it can also be called before the function is defined);
<script>
    
    test()
    function test(){
        console.log('function can be printed when called above')
    }    

</script>

4. Variable declaration, declaration promotion (that is, only the previous var text is promoted, the second half belongs to the assignment and cannot be promoted, so it will not report an error, but as precompiled by var text);

<script>
    console.log(text);    //undefined
    var text = 'abc'

</script>


2. Pre-compilation process

Precompilation happens just before the function is executed

The 4 steps of pre-compilation:
1. Create AO
2. Find the formal parameter and variable declaration, use the formal parameter name as the property name of AO, and the value is undefined
3. Unify formal parameters and actual parameters
4. Find the function declaration in the function body, and assign the value to the function


The following examples

function fn(a){

            console.log(a);                          //function a(){}

            var a=123;

            console.log(a);                          //123

            function a(){};

            console.log(a);                           //123

            var b=function(){};

            console.log(b);                           //function(){}

            function d(){};                              //function d(){}

            console.log(d);
        }

        fn(1);

        // first print second print third print
         // AO{ (2 formal parameters and variable declarations) (3 formal parameters corresponding to actual parameters) (4 function declarations are directly promoted) (assigned in sequence)
         //     a:undefind;-----------------1-------------------function a(){}---------------123
         // (fourth print)
         //     b:undefind;---function(){}
         // (fifth print)
         //     d:undefind;---function d(){}

         // }


//G0{
         //a:undefind;
         //c:123;
         //}
        function test(){
            console.log(b);   //undefind
            if(a){
                var b=100;    //if不成立   a=undefind

            }
            console.log(b);   //undefind

            c=123;           //AO里找不到  给全局
            console.log(c);   //123
        }

        var a;
        test();
        //AO{
        //b:undefind   

        //}
        a=10;    //此时a改变了
        //G0{
         //a:10;
         //c:123;
         //}                 
        console.log(a);    //10
        console.log(c);    //123


  3.
     //    GO{
     //    demo:function(){};
     //    a:100
     //    f:123
     //    }

         a=100;
         function demo(e){
             function e(){}
             arguments[0]=2;  
             console.log(e);   //2

             if(a){           //此时为unefind  if语句不成立
                 var b=123;
             }
             var c;           
             a=10;
             var a;                //10
             console.log(b);       //unefind
             f=123;                //给全局
             console.log(c);       //undefind
             console.log(a);       //10

         }
         var a;
         demo(1)
         //A0{
         //e:funcion e(){};   2
         //b:undefind;
         //c:undefind;
         //a:undefind;    10
         //}
         console.log(a);         //100
         console.log(f);         //123



看不懂的可以在腾讯课堂上搜索渡一教育的js课,老师讲的挺好,也挺详细。


Guess you like

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