js预编译
1.js运行三部曲
语法分析 --> 预编译 -->解释执行
语法分析 --> 预编译 -->解释执行
1.1预编译
① 预编译前奏 —— 全局和局部的概念
console.log(a);
function a(a){
var a=234;
function a(){
}
}
var a=123;//全局和局部的概念
1.任何变量,如果变量未经过声明就赋值,次变量就归全局所有
eg:a=123;
var a = b = 123;
output: b:123
a:123
2.一切的全局变量,全是windows属性。
eg:var a=123;===>window.a=123;
②预编译
1.创建AO对象(执行期上下文 )//存储空间安库
2.找形参和变量声明,将变量和形参作为AO属性名,值为undefind
3.将实参和形参统一,
4.在函数体里面找函数声明,值赋予函数体
例一:
AO{
a:1,
b:undefind,
d:undefind
}
function a(){
console.log(a);//2
var a;
a=123;
console.log(a);
var b=function(){
}
function a(){}
var c=234;
console.log(b)
console.log(c)
}
AO:{
a:undefind =>2=>function a(){}=>123
b:undefind,
c:undefind=>234
}
a(2); //总结:1、预编译发生在函数执行的前一刻 2、解决函数执行顺序问题 函数声明整体提升,变量提升
例二:
function test(a,b){
console.log(a);
c=0;
var c;
a=3;
b=2;
console.log(b);
function b(){}
function d(){}
console.log(b)
}
AO:{
a:3;
b:2;
c:0;
d:function(){}
}
test(1);
例三:
a = 100;
function demo(e){
function e(){}
arguments[0] = 2;
console.log(e);//2
if(a){//a undefind,所以b未被定义=>undefind
var b=234;
function c(){}
}
var c; //undefind
a= 10;
var a;
console.log(b);
f = 123;
console.log(c);
console.log(a);//10
}
var a;
demo(1);
console.log(a);
console.log(f);
第一步:GO:{
a:100,
demo:function(){}
f:123
}
第二部到AO:{
找到变量声明 形参实参统一 函数提升
e:2
b:undefind
c:undefind
a:10
}
挑战性作业:
//隐式类型转换 false转换为字符串类型默认为0 + 1 = 1
var str=false + 1;
console.log(str);//0+1=>1
var demo=false==1;//判断相等是否等于1
console.log(demo);//false
if(typeof(a) && -true + (+undefined) + ""){//"undefind" + "NAN" true
"undefind" + "NAN" + "NAN"
console.log('基础扎实');
}
if(11 + '11' * 2 == 33){//乘法两边都变成数字 运算符两侧都变成数字
console.log('扎实');
}
//true + false - !!false || console.log(' 你觉得能打印,你就是猪 ')''
// 1 + 0 - 0 ==>1 || 正确后面不执行
// !!" " + !!"" - !!false || console.log(' 你觉得能打印,你就是猪 ')''