预编译面试题!

14 篇文章 0 订阅
本文深入解析JavaScript中的执行上下文(Execution Context)概念,包括全局上下文和函数上下文,并详细阐述预编译过程,包括变量对象(AO/GO)的创建、变量与函数声明的处理。通过实例分析,帮助读者理解函数调用时的环境构建及作用域链的形成,同时介绍了预编译对于this值的确定以及未声明变量的处理规则。
摘要由CSDN通过智能技术生成

预编译面试题!

1、预编译知识点(引用)

首先,做这类题目需要掌握四个知识点:

(1)、执行期上下文:

当函数在执行前一刻,会创建一个执行期上下文的内部对象。一个执行期上下文定义了一个函数执行时的环境(1.初始化函数的参数arguments,AO,2.创建作用域链3.确定this的值),函数每次执行时对应的执行上下文一定是独一无二,所以多次调用一个函数会导致创建多个执行期上下文,当函数执行完毕,它所产生的的执行上下文被销毁。

(2)、函数预编译(AO):
从函数执行的前一刻开始:
2.1、创建一个函数的AO对象(Activation Object),执行期上下文对象;
2.2、函数的形参,成为AO对象的属性,值为实参的值,若未传值,值为undefined
2.3、将var关键字声明的变量,成为AO对象的属性,值为undefined,遇到重名,不做任何变化;
2.4、将function声明的函数(函数声明)成为AO对象的属性,值为函数体,重名直接覆盖;

(3)、全局预编译(GO):
3.1、创建一个函数的GO对象(Global Object),执行期上下文对象;
3.2、将var关键字声明的变量,成为AO对象的属性,值为undefined,遇到重名,不做任何变化;
3.4、将function声明的函数(函数声明)成为AO对象的属性,值为函数体,重名直接覆盖;

(4)、预编译前奏:
4.1、 imply global : 暗示全局变量:即任何变量,如果变量未经声明就赋值,此变量归全局(window)所有;
4.2、一切声明的全局变量,全是window的属性;

2、做题思路小结

其次,要分析每一步的情况(分三步走):

(1)、当有了script标签就会出现GO环境;分析全局中声明varfunction
添加至GO当中,注意上面的GO中的三句话,还有将所有未声明直接赋值的给到全局变量;

(2)、从函数执行的前一刻开始,就会出现AO环境分析全局中声明varfunction 添加至AO当中,注意上面的GO中的三句话,还有将所有未声明直接赋值的给到全局变量;

(3)、由上到下由左往右,执行代码,(有赋值有传参需要重点注意),分析每一步有没有赋值情况,没有的话找AO拿,AO没有则找GO拿,自己有则不找别人要;

3.例题如下(代码表示):

最后,纵观全局和局部,看特殊一步步走:
(具体题目具体分析)

eg1:

function fn(a){       // 传值  a:1
        console.log(a);      //fn

        var a = 123;        //   AO   a:undefined

        console.log(a);      //123

        function a(){}         //  AO   a: function a(){}

        console.log(a);       //123

        var b =function(){}    // b : undefined;

        console.log(b);         //fn

        function d(){}           // AO   d : function d(){}
        }
        fn(1);
    // 首先分析一下GO
    // GO{
    //     全局变量只用分析全局预编译
    //     a:function a(){}
    //     
    // }
    // AO{
    //     当函数调用时出现局部变量预编译
    //     a:1(被覆盖)
    //     (a:undefined,(被覆盖))
    //     a:function a(){},
    //     b:undefined,
    //     d:function d(){}

    // }

eg2:

 function test(a,b) {       //传值1
              console.log(a);    //1
              c = 0;            // AO   undefined
              var c ;           //c:0
              a = 3 ;           // AO   undefined
              b = 2 ;            // AO   undefined
              console.log(b);    // 2
              function b(){ }    
              function d(){ }
               console.log(b);   //2
                console.log(d);  //function d(){}
              }  
              test(1);
 // 首先分析一下GO
    // GO{
    //     全局变量只用分析全局预编译
    //     test:function test(){}
    //      c:undefined
    //      a:undefined
    //      b:undefined
    // }
    // AO{
    //     当函数调用时出现局部变量预编译
    //     a:1
    //     c:undefined
    //     b:function b(){}
    //     d:function d(){}

    // }

eg3:

 function test(a,b){
                 console.log(a);    //function a(){}
                 console.log(b);    //undefined
                 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);     //function (){}
              }
              test(1);
// 首先分析一下GO
    // GO{
    //     全局变量只用分析全局预编译
    //     test:function test(){}
    //      直接 赋值未声明直接给到全局变量
    //      a:undefined
    //      b:undefined
    // }
    // AO{
    //     当函数调用时出现局部变量预编译
    //     c:undefined
    //     b:undefined
    //     a:function a(){}

    // }

eg4:

      global = 100;
        function fn() { //未传值
            console.log(global);//undefined  找AO要
            global = 200;
            console.log(global);//200
            var global = 300;

        }
        fn();
        var global;
// 首先分析一下GO
    // GO{
    //     全局变量只用分析全局预编译
    //     fn:function fn(){}
    //      直接 赋值未声明直接给到全局变量
    //      global:undefined(不变)\200
    //      
    // }
    // AO{
    //     当函数调用时出现局部变量预编译
    //     global:undefined
    

    // }

eg5:

  function bar() {
            return foo;
            foo = 10;
            function foo() { }
            var foo = 11;
        }
        console.log(bar()); //function foo(){}看return里面的没有的话找AO要


// 首先分析一下GO
    // GO{
    //     全局变量只用分析全局预编译
    //     bar:function bar(){}
    //      直接 赋值未声明直接给到全局变量
    //      fool:undefined\10

    // }
    // AO{
    //     当函数调用时出现局部变量预编译
    //     foo:function foo(){}
    //     foo:undefined(去掉)
    //     

    // }

eg6:

a = 100;
    function demo(e){
        function e(){}
        console.log(e)  //function e(){}
        arguments[0] = 2;
        console.log(e)  //2
        if(a){
            var b = 123;
            function c(){}
        }
        var c;
        a = 10;
        var a;
        console.log(b)  //undefined
         f = 123;
        console.log(c)  //undefined
        console.log(a)  //10
    }
    var a;
    demo(1)
    console.log(a)  //100
    console.log(f)  //123       没有找全局要
    
    // 首先分析一下GO
    // GO{
    //     全局变量只用分析全局预编译
    //     a:undefined
    //     demo:function demo(){}
    //      直接 赋值未声明直接给到全局变量
    //     a:undefined\100\10(不变)
    //      f:undefined\123

    // }
    // AO{
    //     当函数调用时出现局部变量预编译
     //     e:1(被覆盖)
    //     e:function e(){}
    //     b:undefined
    //     c:function c(){}
    //     c:undefined(去掉)
    //     a:undefined
    // }

4、总结

预编译题目一定要分析透彻,仔细观察每一步代码,结合理论知识和实际例子多加练习一定会掌握明白的,小编初次学习的时候也一遍懵逼,现在好多了哈哈哈哈!!!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值