四部曲:
- 创建AO对象
- 找形参和变量声明,将变量和形参名作为AO属性名,值为undefined
- 将实参和形参统一
- 在函数体里找函数声明,值赋予函数体
function fn(a){
console.log(a);//function 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);
function test(a,b){
console.log(a);//function a () {}
console.log(b);//function () {} × -----> 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);//234 × -------->function () {}
}
test(1);
//运行到b = function () {}才改变值,预编译时不改变值
function test(){
var a = b = 123;
console.log(window.b);
}
test();
// AO{
// a : undefined
// }
// GO{
// b : 123
// function test(){……}
// }
console.log(test);//123 ----->全局变量 × ---------> function test(test){……}
function test(test){
console.log(test);//function test(){}
var test = 234;
console.log(test);//234
function test(){}
}
test(1);
var test = 123;
function test(){
console.log(b);//undefined
if(a){
var b = 100;
}
console.log(b);//undefined------>没有执行if语句,因为a是undefined,预编译发生在函数 //执行前一刻
c = 234;
console.log(c);//234--->c放到GO变量中
}
var a;
test();
a = 10;
console.log(c);//234
console.log(bar());//10 ×----->最后return所以是AO里的foo
function bar(){
foo = 10;
function foo(){}
var foo = 11;
return foo;
}
a = 100;
function demo(e){
function e(){}
arguments[0] = 2;
console.log(e);//2
if(a){//a:undefined
var b = 123;//不执行
}
var c;
a = 10;
var a;
console.log(b);//undefined
f = 123;
console.log(c);//undefined
console.log(a);//10
}
var a;
demo(1);
console.log(a);//100
console.log(f);//123
- 预编译发生在函数执行的前一刻
补:
if(typeof(a)&&-true + (+undefined) + ""){
//"undefined"&&-1 + NaN + ""
//"undefinedNaN"
//True
document.write('OK');
}
- typeof NaN; //'number'
- typeof null; //'object'
- typeof a;//'undefied'
- -true---->-1带符号说明大概率是数字类型
- +undefined----->NaN
if(11 + "11" * 2 == 33){
//11 + 22
//33
document.write('OK');
}
- 除’+‘之外字符串运算都要转为数字。如"11"-"9"=2
!!" " + !!"" - !!false||document.write("ok");
//1+0-0
//1
//或运算1与任何都是1
不会打印
!!" "------>True
!!""-------->False
!!false------>False
整理自腾讯课堂《Web前端JavaScript权威课堂》 渡边教育