javascript---執行上下文(一)
阿新 • • 發佈:2019-02-02
執行上下文 (同名:執行上下文環境),先講述全域性環境下的三種情況;
情況一:
控制檯直接輸入 console.log(a); ==> a is not defind;
控制檯輸入 console.log(a); ==> undefind;
var a;
控制檯輸入 console.log(a); ==> undefind;
var a = 10;
分析:第一句a is not defind;表示沒有a變數,第二句輸出undefind表示a變數定義但是未賦值,說明程式碼執行之前瀏覽器已經做了準備工作(聲明瞭變數a),第三局輸出undefind表示a變數定義並未賦值,說明在程式碼執行之前瀏覽器的準備工作只是宣告變數並不賦值。
情況二:
有js開發經驗的開發者應該知道,無論在哪個位置獲取this都是有值的,所以在執行上下文環境中會直接給this賦值。
情況三:
關於函式,分別在控制檯輸出console.log(f1); function f1(){};和console.log(f2); var f2 = function(){}; ,得到的結果分別為f1函式和undefind,所以函式宣告會在執行上下文環境中宣告並賦值,而函式表示式會在執行上下文環境中宣告該變數。
總結:變數、函式表示式-----變數宣告,預設賦值為undefind;
this-----賦值;
函式宣告-----賦值;