预编译2

46 阅读2分钟

(GO:1.生成全局的Go,2.寻找变量声明 3.函数声明) 预编译的4部曲 //预编译发生在函数执行的前一刻

  1. 创建AO对象 Activation Object(执行期上下文)
  2. 找函数内的形参和变量声明,将变量和形参名作为AO属性名,值为undefined
  3. 将实参值和形参统一
  4. 在函数体里面找函数声明,值赋予函数体

eg1--->

image.png

  1. 创建AO对象
  2. 找函数内的形参和变量声明,将变量和形参名作为AO属性名,值为undefined
  3. 将实参值和形参统一
  4. 在函数体里面找函数声明,值赋予函数体

AO{ a:undefined,--> a:1,--> a:function a(){ } b:undefined,--->b: function b(){ } d:function (){ } }

紧接着执行函数---打印 /预编译看过了就不看了 function a(){ } 123 123 function (){ }

eg2--->

image.png

AO{ a:undefined,-->a:1,-->a:3 b:undefined,-->b:2,-->function b(){ }-->b:2 c:undefined,-->c:0, d:function d(){ } }

//打印 1 2 2

eg3--->

image.png

//打印

image.png

GO /任何全局变量都是window上的属性

image.png

eg4---〉

image.png

//打印 undefined

eg5--->

image.png //变量声明提升了 GO{ global:undefined-->global:100 } 函数执行 有AO AO{ global:undefined--〉global:200 }

打印: undefined 200

eg6---> 预编译不看if

image.png GO{ a:undefined,-->a:10 c:234 }

AO{ b: undefined-->b:100 } //打印 undefined 234 234

 // 1
    function test(a,b){
        console.log(a) //1
        c=0
        var c
        a=3
        b=2
        console.log(b) //2
        function b(){ }
        function d(){ }
        console.log(b) //2
    }
    test(1)
    // AO{
    //     a:undefined,-->a:1,
    //     b:undefined,-->b:2
    //     c:undefined,
    // }

    // 2
    function test(a,b){
        console.log(a) //fn a
        console.log(b) //und
        var b = 234
        console.log(b) //234
        a=123
        console.log(a) //123
        function a(){ }
        var a 
        b=234
        var b =function () { }
        console.log(a) //123
        console.log(b) //fn
    }
    test(1)
    // AO{
    //     a:undefined,-->fn a-->123
    //     b:undefined,-->234-->fn   
    // }

    // 3 
    function bar(){
        return foo ;
        foo = 10
        function foo (){ 

        }
        var foo =11
    }
    console.log(bar()) //fn foo
    // GO{
    //     foo:10
    // }
    // AO{
    //   foo :11-->fn foo
    // }

    // 4 
    console.log(bar()) //11
    function bar(){
        foo = 10
        function foo(){

        }
        var foo = 11
        return foo
    }
    // GO{
    //     foo:10
    // }
    // AO{
    //     foo:undefined-->fn foo-->11
    // }
 //5
    a=100
    function demo(e){
        function e(){}
        arguments[0] = 2
        console.log(e) //2
        if(a){
            var b = 123
            function c(){
            //现在if里面已经不允许定义fn
            }
        }
        var c 
        a = 10
        var a 
        console.log(b) //und
        f = 123
        console.log(c) //und
        console.log(a) //10
    }
    var a
    demo(1)
    console.log(a) //100 函数外拿全局
    console.log(f) //123 函数外拿全局
    
    // GO{
    //  a:und-->100,
    //  f:und-->123
    // }
    // AO{
    //  e:und,-->fn e,-->2
    //  b:und,-->123,
    //  c:und,
    //  a:und-->10,
    // }
 //5
    a=100
    function demo(e){
        function e(){}
        arguments[0] = 2
        console.log(e) //2
        if(a){
            var b = 123
            function c(){
            //现在if里面已经不允许定义fn
            }
        }
        var c 
        a = 10
        var a 
        console.log(b) //und
        f = 123
        console.log(c) //und
        console.log(a) //10
    }
    var a
    demo(1)
    console.log(a) //100 函数外拿全局
    console.log(f) //123 函数外拿全局
    
    // GO{
    //  a:und-->100,
    //  f:und-->123
    // }
    // AO{
    //  e:und,-->fn e,-->2
    //  b:und,-->123,
    //  c:und,
    //  a:und-->10,
    // }