Js预编译 GO和AO活动对象

一,全局预编译 GO

  1. 全局活动对象。
  2. 找变量声明,将变量名作为AO属性名,值为undefined
  3. 找函数声明,优先级同样最高在<script></script>全局预编译阶段

 1、案例一:

console.log(a);//fn
console.log(c);//undefined
var a = 1;
var c = function(){}
function a(){}
console.log(a);//1
console.log(c);//fn


// 1,生成一个GO对象 Global Object
GO {
    a:undefined,  ==> a:1 ==> a:function a(){}
    c:undefined   ==> c:function(){}
}
// 2,生成一个AO对象
AO {//
}

2、案例二:

function test(){
    var a = b = 2;
    console.log(window.a);//undefined
    console.log(a);//2
}
test();

GO === window

// 1,生成GO对象
GO {
    b:2
}

//生成AO对象
AO {
    a:undefined  ==> a:2
}

二,函数预编译 AO活动对象

函数的执行包括

预编译 和 执行

预编译包括四步:

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

1、案例一:

function test(a,b){
        console.log(a);//fn
        console.log(b);// undefined
        a = 2;
    var a;
    var b = function b() {
    };
    function a(){};
    console.log(a);//2
        console.log(b);//fn
};
test(1);

函数预编译时AO对象属性值的变化过程:(包括变量提升)
//找出函数内的形参和变量,将其设置为AO对象的属性,并设置值为undefined
AO {
   a:undefined,
   b:undefined
}
//实参传入,优先级为1
AO {
   a:1,
   b:undefined
}
//在函数体内找函数声明,函数声明的优先级最高 <-- 在预编译阶段
AO {
   a:function a(){},
   b:undefined
}
从下到下执行,遇到函数声明与变量声明跳过(此阶段已经在预编译阶段完成)

 

posted @ 2021-04-21 15:18  千叶祥龙  阅读(125)  评论(0编辑  收藏  举报