js 預編譯
阿新 • • 發佈:2022-01-06
變數宣告提升
函式整體提升
函式體預編譯步驟:1、建立AO物件
2、找形參和變數宣告,將變數和形參名作為AO屬性名,值為undefined
3、將實參值和形參對應賦值
4、在函式體裡面找函式宣告,函式名為作為AO屬性名,值為函式體
函式體預編譯在函式執行之前執行
function fn(a){ console.log(a);//function a(){} var a=123; console.log(a) //123function a(){}//此處是函式宣告 console.log(a) //123 var b=function(){}//此處不是函式宣告 不函式表示式 console.log(b) //function(){} function d(){} } fn(11) //1、建立AO物件 AO={ } 2、找形參和變數宣告,將變數和形參名作為AO屬性名,值為undefined AO={a:undefined ,b:undefined }//3、將實參值和形參對應賦值 AO={a:11 ,b:undefined } 在函式體裡面找函式宣告,函式名為作為AO屬性名,值為函式體 AO={a:function a(){} ,b:undefined } //函式執行時 // 1、console.log(a); console.log(AO.a) 結果是function a(){} // 2、var a=123; AO={a:123 ,b:undefined } console.log(a)console.log(AO.a) 結果是123// 3、function a(){}//此處是函式宣告 已提升不用管 AO={a:123 ,b:undefined } console.log(a)console.log(AO.a) 結果是123 // 4、var b=function(){} //此處不是函式宣告 不函式表示式AO={a:123 ,b:function(){} } console.log(b)console.log(AO.b) 結果是function(){} function test(a,b){ console.log(a)//12 c=0 var c a=3 b=2 console.log(b) //2 function b(){} function d(){} console.log(b) //2 } test(12) function test1(a,b){ console.log(a) //function 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) //function(){} } test1(13)