JS中预编译是指在程序开始前一刻执行的。
预编译一般分为四步
在函数预编译中
1.生成AO(函数内)
2.放入形参、变量声明,值为undefined。
3.形参实参相对应
4.放入函数声明 值为函数本身
例如:
在下列函数中
<script>
function a( b ) {
console.log(b)
console.log(c)
console.log(d)
function c() {}
b = 300
console.log(b)
function b(){}
var d
}
a(1)
</script>
生成AO文件
AO{
}
放入形参、变量声明,值为undefined
AO{
b: undefined
d: undefined
}
形参实参相对应
AO{
b: 1
d: undefined
}
函数声明 值为函数本身
AO{
b: function b() {}
d: undefined
c: function c() {}
}
到此预编译结束
然后开始进行函数运行,按顺序执行语句
console.log(b)//function b() {}
console.log(c)//undefined
console.log(d)//function c() {}
b = 300
此时
AO{
b: 300
d: undefined
c: function() {}
}
console.log(b)//300
在整个js中预编译过程与函数中一致,只是没有了放入形参、形参实参相对应的一步。全局先生成GO文件而不是AO文件。
另有一知识点补充
b = 2
function test(){
var a = c = 3
console.log(a)
}
test()
console.log(b)
console.log(c)
console.log(a)
在上述代码中
b = 2
相当于在全局中创建一个b。window.b = 2。 window是存放全局元素的。
同理
函数中的 var a = c = 3
按照规则先运行 c = 3 ,直接在window下挂了一个c。即window.c = 3,c属于全局的值。放在GO文件中。
再运行 a = c ,a属于函数内部的元素,放在AO文件中。
在JS中函数外不能访问函数内特有的参数。
第一个为a**(函数内部打印)**
第二个为b
第三个为c
第四个为a**(因为外部不能访问内部,所以函数报错。)**