深入理解scope

深入理解scope

一. 简单的例子

	var a = 1;
	function fn(a){
	     console.log(a)  
	     var a = 2;
	 }
	 fn(3)
	 显而易见,结果是3var a = 1;
	 function fn(a){
	     console.log(a)  
	     a = 2;
	 }
	 a = 10;
	 fn()  
	 答案是undefined,如果答案不对的话,你的函数预编译需要好好学一下。预编译

二. 执行的过程

	 function a(){
	    function b(){
	         function c(){
	         }
	         c()
	     }
	     b()
	 }
	 a()

1,调用a()函数之前,会有一个初始化的时候。这时候调用一个[[scope]],里面有GO对象。

[[scope]]:每个javascript函数都是一个对象。对象中有些属性我们可以访问,有些不可以访问,他们只供javascript引擎存取。

[[scope]]就是其中一个,[[]]指的就是我们所说的作用域,其中存储了运行期上下文集合。

作用域链:[[scope]]中所存储的执行期上下文对象的集合,这个集合呈链式结构,就叫做作用域链[[scope chain]]

  a().[[scope]]={
   这里是当前作用域; 
        [[scope chain]]={
            这里是作用域链
              GO:{
                this:window,
                window:object,
                document:object,
                a:function
            }
        }
    }

执行期时,在执行的前一刻,会有一个执行期上下文。创建完成后,把执行期上下文放到作用域链[[scope chain]]上面,他们共同组成的新对象叫活跃对象。

执行期上下文:当函数执行前一刻。会创建一个称为执行期上下文的内部对象(AO),每个执行期上下文都是独一无二的,当函数 执行完后,就自动销毁。

a().[[scope]]={  
    [[scope chain]]={
        AO:{
            this:window,
            argument:[],
            b:function
        },
        GO:{
            this:window,
            window:object,
            document:object,
            a:function
        }
    }
}

在这里插入图片描述

2.在继续调用b函数,b函数也会初始化,也调用[[scope]]。

   b().[[scope]]={    
        [[scope chain]]={
            AO:{
                this:window,
                argument:[],
                c:function
            },
            GO:{
                this:window,
                window:object,
                document:object,
                a:function
            }
        }
    }

在执行前创建b函数的执行期上下文,放到作用域链上面,放上去后将作用域链的活跃对象提升到[[scope]]

 b().[[scope]]={ 
    [[scope chain]]={
        AO:{
            this:window,
            argument:[],
            c:function
        },
        AO:{
            this:window,
            argument:[],
            b:function
        },
        GO:{
            this:window,
            window:object,
            document:object,
            a:function
        }
    }
}

在这里插入图片描述

3.在继续调用c函数,c函数也会初始化,也调用[[scope]]。

  c().[[scope]]={    
       [[scope chain]]={
           AO:{
               this:window,
               argument:[],
               c:function
           },
           AO:{
               this:window,
               argument:[],
               b:function
           },
           GO:{
               this:window,
               window:object,
               document:object,
               a:function
           }
       }
   }

在执行前创建b函数的执行期上下文,放到作用域链上面,放上去后将作用域链的活跃对象提升到[[scope]]

b().[[scope]]={
	[[scope chain]]={
	    AO:{
	        this:window,
	        argument:[]
	    }
	    AO:{
	        this:window,
	        argument:[],
	        c:function
	    },
	    AO:{
	        this:window,
	        argument:[],
	        b:function
	    },
	    GO:{
	        this:window,
	        window:object,
	        document:object,
	        a:function
	    }
	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值