预编译scope链

一、预编译

预编译阶段:就是作用域的创建阶段

作用域分为全局作用域函数作用域
函数作用域创建阶段有一个 js的变量对象(AO对象)供js引擎自己去访问的

预编译做了哪些事情?
提升:名字相同时函数声明在变量声明前面

  1. 函数声明提升

  2. var a 变量提升

  3. 值在执行到该行才进行赋值

  4. 创建ao对象

  5. 找形参和变量的声明 作为ao对象的属性名 值是undefined

  6. 实参和形参相统一

  7. 找函数声明 会覆盖变量的声明

function fn(a, c) {
	console.log(a)  //function a() { }
    var a = 123      
    console.log(a)  //123
    console.log(c)  //function c() { }
    function a() { } //函数声明
    if (false) {
    	var d = 678
    }
    console.log(d)  //undefined
    console.log(b)  //undefined
    var b = function () { } //函数的表达式
    console.log(b)   //function () { } 
    function c() { } //函数声明
    console.log(c)  //function c() { }
}
fn(1, 2)

//预编译阶段
AO:{     2          3          4
	a:undefined     1     function a() { } 
	c:undefined     2     function c() { }
	d:undefined
	b:undefined
} 
//js逐行执行

js scope链

  1. 所有的function和var提前进行声明
  2. 但是并不会对其进行赋值
  3. 赋值则都是在该代码块进行执行时才会对其进行赋值
  4. 全局为一个scope,一个function为一个scope
  5. doit执行时才会创建新的Scope
    所以为:
    scope:b = 3
function doit(){
    var b
    console.log(b); // undefined
    b = 3;
    console.log(b); // 3
}

scope:a=1,b=2,function doit()
b=3

var a = 1;
var b = 2;
function doit(){
    console.log(b); // undefined
    var b = 3;
    console.log(b); // 3
}
doit();
console.log(b); // 2

scope: a=1,b=2,function doit()
b = 3

var a = 1;
var b = 2;
function doit(){
    console.log(a); // 1
    // 重新声明了b
    console.log(b); // undefined
    var b = 3;
    console.log(b); // 3
    console.log(a); // 1
}
doit();
console.log(b); // 2

a向上检索 没有找到时就为undefined

var a = 1;
var b = 2;
function doit(){
    console.log(a); // 1
    console.log(b); // 2
    b = 3;
    console.log(b); // 3
    console.log(a); // 1
}
doit();
console.log(b); // 2

scope链,匿名函数的scope,object的,this的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值