JavaScript 预编译的四个步骤
用 AO 和 GO 梳理 JavaScript 函数预编译的基本过程。
函数预编译
- 创建 AO 对象
- 找形参和变量声明作为 AO 属性,值都为
undefined - 实参值和形参值统一
- 在函数体内找函数声明,值赋予 AO 函数名
示例
function hong(a, b) {
console.log(a)
c = 0
var c
a = 3
b = 2
console.log(b)
function b() {}
function d() {}
console.log()
}
hong(1)
1. 创建 AO 对象
AO {
}
2. 找形参和变量声明
AO {
a: undefined,
b: undefined,
c: undefined
}
3. 实参值和形参值统一
AO {
a: 1,
b: undefined,
c: undefined
}
4. 找函数声明
AO {
a: 3,
b: 2,
c: 0,
d: function d() {}
}
// console.log()
1
2
2
全局预编译
全局预编译时会生成 GO === window,并且没有第三步。