預處理:創建一個詞法環境(LexicalEnvironment,後文簡寫為 LE),掃描 JS 中用聲明方式聲明的函數,以及用 var 定義的變量,並把它們加到預處理階段的詞法環境中。
比如這段代碼:
var a = 1;// 用 var 定義並賦值
var b; // 用 var 定義,未賦值
c = 3; // 未定義,直接賦值
function d(){// 用聲明方式聲明的函數
console.log('hello');
}
var e = function(){// 函數表達式
console.log('world');
}
預處理時創建的詞法作用域可以這樣表示:
LE{
a: undefined
b: undefined
// 沒有 c
d: 對函數的一個引用
// 沒有 e
}
強調兩點:
1、預處理的函數必須是用聲明方式聲明的函數(不是函數表達式):
d();
e();
function d{// 用聲明方式聲明的函數
console.log('hello');
}
var e = function(){// 函數表達式
console.log('world');
}
輸出結果分別是:hello;以及報錯 e is not a function。
2、預處理的變量必須是用 var 定義的:
console.log(a);// undefined console.log(b);// undefined console.log(c);// 報錯:c is not defined var a = 1; var b; c = 3;
看下面的代碼,你覺得輸出是什麼?
console.log(f);
var f = 1;
function f(){
console.log('foodoir');
}
console.log(f);
function f(){
console.log('foodoir');
}
var f = 1;
console.log(f); var f = 1; var f = 2;
console.log(f);
function f(){
console.log('foodoir');
}
function f(){
console.log('hello world');
}
很多人第一反應會答錯,關鍵在於要理解前面的預處理。四段代碼的結果依次為:
第一段輸出:function f(){console.log('foodoir');}。第二段雖然 JS 函數沒有傳統意義的重載,但結果仍然是 function f(){console.log('foodoir');}。第三段結果為 undefined。第四段結果為 function f(){console.log('hello world');}。
原因:處理函數聲明衝突時會覆蓋,處理變量聲明衝突時會忽略。當函數聲明與變量聲明同時存在時,可以像 CSS 權重那樣理解——函數聲明權重更高,最終往往指向函數聲明的引用。
console.log(a);
console.log(b);
console.log(c);
console.log(d);
var a = 1;
b = 2;
console.log(b);
function c(){
console.log('c');
}
var d = function(){
console.log('d');
}
console.log(d);
1、先分析全局預處理:
LE{
a: undefined
// 沒有 b
c: 對函數的一個引用
d: undefined
}
此時前四行結果依次為:undefined、報錯、function c(){console.log('c');}、undefined。
2、預處理完成後,代碼逐行解析(把第二行報錯的代碼註釋掉)。第 6 行執行完,LE 中 a 變為 1;第 7 行執行完,b 成為全局變量且值為 2;第 10 行執行完,c 指向函數;第 14 行執行完,d 指向函數。關於 b 變為全局變量,可在控制台輸入 window.b 得到 2:
補充:利用詞法作用域,可以解釋帶多個參數的函數只傳遞一個參數的情況:
function f(a, b){ }
f(1);
它的詞法作用域為:LE{ a:1, b:undefined }。
函數中的解析和執行與全局類似,但要注意 arguments:
function f(a, b){
alert(a);
alert(b);
var b = 100;
function a(){}
}
f(1, 2);
預處理的詞法結構如下:
LE{
b: 2
a: 指向函數的引用
arguments: 2 // 調用函數時實際傳入的參數個數
}
結合「函數聲明衝突會覆蓋、變量聲明衝突會忽略」,結果分別為 function a(){} 和 2。
當只傳入一個參數時:
function f(a, b){
alert(a);
alert(b);
var b = 100;
function a(){}
}
f(1);
詞法結構為:LE{ b:undefined, a:對函數的一個引用, arguments:1 },結果分別為 function a(){} 和 undefined。
還要注意:沒有用 var 聲明的變量,會變成最外部 LE 的成員,即全局變量:
function a(){
function b(){
g = 12;
}
b();
}
a();
console.log(g);// 12
控制台結果:
console.log(a);// undefined
console.log(b);// undefined
console.log(c);// c is not defined
console.log(d);// d is not defined
var a = 1;
if(false){
var b = 2;
}else{
c = 3;
}
function f(){
var d = 4;
}
前三個容易得出,第四個卻值得深究。在編程語言中,作用域一般分为四類:塊級作用域、函數作用域、動態作用域、詞法作用域(也稱靜態作用域)。
其他 C 系語言中,大括號括起來的部分是作用域,但 JavaScript 沒有塊級作用域:
for(var i=0; i<3; i++){
// ...
}
console.log(i);
結果為 3,因為循環結束後 i 仍有效。
JavaScript 沒有純粹的函數作用域。
function f(){
alert(x);
}
function f1(){
var x = 1;
f();
}
function f2(){
var x = 1;
f();
}
f1();
f2();
若存在動態作用域,結果應為 1、2,但實際結果是 x is not defined。所以 JavaScript 沒有動態作用域。
在函數最前面聲明一個 x=100:
var x = 100;
function f(){
alert(x);
}
function f1(){
var x = 1;
f();
}
function f2(){
var x = 1;
f();
}
f1();
f2();
結果彈出兩次 100,說明 JavaScript 採用靜態作用域。在詞法解析階段,作用域就已經確定了,並形成作用域鏈。看下面的例子:
function f(){
// f.scope == window
var x = 100;// f.LE == {x:100, g:函數}
var g = function(){// g.scope = f.LE
alert(x);
}
g();// 先找 g.scope,沒有再找 f.LE,還沒有再找 f.scope……一直往上到 window
}
f();
最終結果為 100。再看一個經典例子:
var color = "red";
function changeColor(){
var anotherColor = "blue";
function swapColor(){
var tempColor = anotherColor;
anotherColor = color;
color = tempColor;
console.log(color); // blue
console.log(anotherColor); // red
console.log(tempColor); // blue
}
swapColor();
console.log(color); // blue
console.log(anotherColor); // anotherColor is not defined
console.log(tempColor); // tempColor is not defined
}
changeColor();
console.log(color); // blue
console.log(anotherColor); // anotherColor is not defined
console.log(tempColor); // tempColor is not defined
另外,new Function 的情況又不同:
var x = 123;
function f(){
var x = 100;
// g.[[scope]] == window
var g = new Function("", "alert(x)");
g();
}
f(); // 結果為:123
小結:以 f1{ f2{ x }} 為例,取 x 時先在自己的詞法環境找,找不到去父級函數的詞法環境找……一直到 window。
由此衍生出幾個實用問題:
問題 1:多個函數都想用一個變量,每次都寫很麻煩,怎麼偷懶?方法:把變量設為全局變量。
問題 2:大項目要引入多個 JS 庫,全局變量容易命名衝突且出錯後難排查,怎麼辦?方法:把變量放在一個大 function 中:
function(){
var a = 1;
var b = 2;
function f(){
alert(a);
}
}
問題 3:這樣外面又訪問不到了,怎麼辦?方法:使用匿名函數並把需要暴露的挂到 window 上:
(function(){
var a = 1, b = 2;
function f(){
alert(a);
}
window.f = f;
})();
f(); // 結果為:1