1.imply global 暗示全局变量:即任何变量,如果变量未声明即赋值,此变量就为全局对象所有
- a=123;
- var a = b =123;
2.一切声明的全局变量,全是Windows属性。
- var a = 123;==>window.a=123;
预编译过程
<script type="text/javascript">
function fn(a){
console.log(a);//ƒ 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对象(Activation Object)(执行其上下文)
2.找形参和变量声明,将变量和形参名作为AO属性名,值为undefined
AO{
a:undefined,
b:undefined,
}
3.将实参和形参统一
AO{
a:1,
b:undefined,
}
4.在函数体里找到函数声明,值赋予函数体
AO{
a:function a (){},
b:undefined,
d:function d (){}
}
*/
</script>
预编译练习
function test(a,b){
console.log(a)//1
c=0;
var c;
a=3;
b=2;
console.log(b);//2
function b(){}
function d(){}
console.log(b);//2
}
test(1);
// 1.AO{
// a:undefined,
// b:undefined,
// c:undefined,
// }
// 2.AO{
// a:1,
// b:function b(){},
// c:undefined,
// d:function d(){}
// }
// 3.AO{
// a:3,
// b:2,
// c:0,
// d:function d(){}
// }
function test(a,b){
console.log(a);//f a(){}
console.log(b);//undefined
var b =234;
console.log(b)//234
a = 123;
console.log(a)//123
function a(){}
var a;
b = 234;
var b = function(){}
console.log(a);//123
console.log(b);//f(){}
}
test(1)
// 1.AO{
// a:1,
// b:undefined
// }
// 2.AO{
// a:function a() {},
// b:234
// }
// 3.AO{
// a:123,
// b:234,
// }
// 4.AO{
// a:123,
// b:function(){}
// }