webveuje/js/kejian/day4yubianyi.md
2021-02-02 17:27:36 +08:00

70 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 预编译
### 预编译类型
* 全局预编译 GO
* 局部预编译 AO
### 预编译发生的时间点
* 全局预编译 发生在 整个页面加载完成
* 局部预编译 发生在 函数执行的前一刻
### 预编译过程
* GO:
1.先找变量和函数声明 赋值为undefined
2.扫描执行后 变量赋值
3.寻找函数声明 并把函数体赋给函数
4.执行
* AO
1.先找变量和形参
2.实参和形参相统一
3.寻找函数声明 并把函数体赋给函数
4.执行
函数表达式会在实参和形参相统一的步骤中完成赋值
```
function a (val) {
var name = val
// c=0 这里的c是全局变量 window.c 能访问到
function b () {}
var d=function(){} //d是一个函数表达式
}
var str = 'Hello World'
a(1)
```
分析:
GO:
1.找变量和函数声明 a,str 并赋值为Undefined
Go{
a:undefined,
str:undefined
}
2.执行的过程中 变量赋值
GO{
a:undefined
str:"hello world"
}
3.找函数声明
GO{
a:function(){}
str:"hello world"
}
Ao:
1.先找变量和形参
AO{
val:undefined,
name:undefined
d:undefined
}
2.实参和形参相统一
AO{
val:1,
name:1
d:function(){}
}
3.寻找函数声明 并把函数体赋给函数
AO{
val:1,
name:1,
b:function(){}
d:function(){}
}