在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ JS語法作用域問題。

JS語法作用域問題。

最近在看JAVASCRIPT語言精粹與編程實踐這本書。
書中講了語法作用域

clipboard.png
clipboard.png

自己做了一下測試,效果好像不對。

clipboard.png
程序直接報錯,func1 is not a function。
函數(shù)func2可以執(zhí)行,因為對函數(shù)做了提升。
那么問題到底出在了哪里?各位大佬。

回答
編輯回答
編輯回答
吢丕

說簡單簡單,說復雜復雜的
你把func1的定義放在判斷之后,示例中也是這樣的,這里涉及到哪種類先執(zhí)行的問題

if(true){
    function func1(){
        console.log(56)
    }
}
func1()
2018年1月12日 18:02
編輯回答
涼薄

這只是和寫書的人的時間有關(guān)系,有些書會告訴你這個規(guī)則正在(已經(jīng))改變。這里所隱藏的是函數(shù)聲明提升方式。

以下為ECMAScript標準,僅為了解釋上訴錯誤,在任何情況下都不建議使用此標準。

Function statements are NOT declared during variable instantiation. They are declared at run time, just like function expressions. However, once declared, function statement's identifier becomes available to the entire scope of the function. This identifier availability is what makes function statements different from function expressions (you will see exact behavior of named function expressions in next chapter).
console.log(typeof foo); // "undefined"
if (true) {
  // once block is entered, `foo` becomes declared and available to the entire scope
  function foo() { return 1; }
} else {
  // this block is never entered, and `foo` is never redeclared
  function foo() { return 2; }
}
console.log(typeof foo); //

http://kangax.github.io/nfe/#...

2017年7月14日 22:57