预编译
预编译阶段:作用域的创建阶段
function fn(a,c){
console.log(a);
var a = 123;
console.log(a);
console.log(c);
function a(){};
if(false){
var d = 678;
}
console.log(d);
console.log(b);
var b = function(){};
console.log(b);
function c(){};
console.log(c);
}
fn(1,2);
预编译阶段所经历的过程:
- 创建AO对象;(AO对象:供JS引擎访问)
- 找形参和变量的声明(作为AO对象的属性名,值是undefined);
- 实参和形参相统一;
- 找函数声明(会覆盖变量的声明)。
AO对象状态:
| 变量名 | 步骤2 | 步骤3 | 步骤4 |
|---|---|---|---|
| a | undefined | 1 | function a(){} |
| c | undefiend | 2 | function c(){} |
| b | undefined | ||
| d | undefined |
运行结果:
function a(){} 123 function c(){} undefined undefined function (){} function c(){}