预编译
【预编译发生在函数执行的前一刻】
函数体系里的预编译:
eg:function fn(a){
console.log(a); //返回:function a(){}
var a = 123;
console.log(a); //返回:123
function a() {}
console.log(a); //返回:123
var b = function(){}
console.log(b); //返回:function(){}
function d() {}
}
fn(1);
步骤:
1.先执行以下四部曲,将AO对象求出来
四部曲:
1>创建AO对象(Activation Object 执行期上下文)
eg:AO{ }
2>找形参和变量声明,将变量和形参名作为AO属性名,值为undefined。
eg:AO{
a:undefined,
b:undefined,
}
3>将实参值和形参统一
eg:AO{
a:1,
b:undefined,
}
4>在函数体里面找函数声明,值赋予函数体
eg:AO{
a:function a(){},
b:undefined,
d: function d(){}
}
2.根据AO对象进行逐步分析
3.第一个a :返回AO对象里的a
4.第二个a :得先将a赋值为123之后输出
5.第三个a :返回的还是123(因为function a(){}已经被预编译了,所以就不用看了)
6. b :返回function(){} (因为预编译只编译了前面的,所以后面的得传进去)
全局预编译 (window === GO)
三部曲:
1>创建GO对象(Global Object 执行期上下文)
eg:GO{ }
2>找形参和变量声明,将变量和形参名作为AO属性名,值为undefined。
3>在函数体里面找函数声明,值赋予函数体