「this 指的是當前物件」——這個說法在大多數情況下沒錯,卻不足以解釋所有現象。要真正掌握 this,關鍵是理解它取決於「函式如何被呼叫」,而不是「函式在哪裡定義」。
常見寫法中,this 確實指向當前元素:
<input type="submit" value="提交" onclick="this.value='正在提交數據'" />
但下面這段就不那麼直觀了:
var foo = function () { console.log(this); };
foo();
new foo();
比較兩次呼叫:foo() 中的 this 指向 window,而 new foo() 中的 this 才真正指向 foo 產生的物件。
原因牽涉到 JavaScript 的作用域與閉包機制。this 的值若未經指定,就是函式「當前所處的作用域」。
<input type="button" id="aButton" value="demo" />
<script>
function demo() {
this.value = Math.random();
}
</script>
直接呼叫 demo() 會報錯,因為 demo 定義在全域作用域中,其擁有者是 window,而 window 沒有 value 屬性。

改成把函式的「副本」掛到元素上,擁有者就換人了:
document.getElementById("aButton").onclick = demo;
此時 aButton 的 onclick 是 demo 的一個副本,this 也指向 aButton。

你甚至可以為多個元素建立各自的副本,每個副本的擁有者與 this 都各歸其位,互不干擾。

若寫成:
<input type="button" id="aButton" value="demo" onclick="demo()" />
點擊後 again 報錯,this 又回到了 window。因為這種寫法只是「呼叫」函式,並未建立副本。
兩者的實際差異可以直接印出來看:
// 建立副本
<input type="button" id="aButton" value="demo" />
<script>
var button = document.getElementById("aButton");
function demo() { this.value = Math.random(); }
button.onclick = demo;
alert(button.onclick);
</script>
// 輸出:function demo() { this.value = Math.random(); }
// 函式引用
<input type="button" id="aButton" value="demo" onclick="demo()" />
<script>
var button = document.getElementById("aButton");
function demo() { this.value = Math.random(); }
alert(button.onclick);
</script>
// 輸出:function onclick() { demo(); }
前者 onclick 本身就是 demo,後者只是一個會去呼叫 demo 的外殼。

既然副本這麼好用,為何還需要引用?答案是效能。每建立一個副本都要分配記憶體,而實務上多數函式並不會被呼叫,這部分記憶體就白費了;引用方式只需一個指標。折衷方案是把元素作為參數傳入:
<script>
function demo(obj) {
obj.value = Math.random();
}
</script>
<input type="button" value="demo" onclick="demo(this)" />
由於 JavaScript 在執行期才綁定 this,它可能是全域物件、當前物件或任意物件,完全取決於呼叫方式。函式的呼叫方式共四種:作為物件方法、作為普通函式、作為建構函式、透過 apply/call。

判斷步驟可以歸納為:
. 呼叫?是 → this 指向點之前的那個物件。var point = {
x: 0, y: 0,
moveTo: function (x, y) {
this.x = this.x + x;
this.y = this.y + y;
}
};
point.moveTo(1, 1); // 非 new,用 . 呼叫,this 指向 point
function func(x) { this.x = x; }
func(5); // 既非 new 也非 . 呼叫,this 指向 window
x; // 5
var point = {
x: 0, y: 0,
moveTo: function (x, y) {
var moveX = function (x) { this.x = x; }; // this 指向 window
var moveY = function (y) { this.y = y; }; // this 指向 window
moveX(x);
moveY(y);
}
};
point.moveTo(1, 1);
point.x; // 0
point.y; // 0
x; // 1(被寫到了 window 上)
y; // 1
這是 this 最經典的坑:內部函式是獨立呼叫的,this 不會繼承外層。解法是先用一個變數快取 var self = this;,或在 ES5 中使用 bind,ES2015 之後則可直接用箭頭函式(箭頭函式沒有自己的 this,會沿用外層)。
function Point(x, y) {
this.x = x;
this.y = y;
}
var np = new Point(1, 1);
np.x; // 1
var p = Point(2, 2); // 漏了 new
p.x; // undefined
window.x; // 2(污染了全域物件)
function Point(x, y) {
this.x = x;
this.y = y;
this.moveTo = function (x, y) {
this.x = x;
this.y = y;
};
}
var p1 = new Point(0, 0);
var p2 = { x: 0, y: 0 };
p1.moveTo.apply(p2, [10, 10]); // 等同 p2.moveTo(10, 10)
p2.x; // 10
apply 與 call 允許切換函式的執行上下文,也就是強制指定 this 綁定的物件,這正是它們強大的原因。
關於函式執行環境,一段描述值得細讀:
「JavaScript 中的函式既可以被當作普通函式執行,也可以作為物件的方法執行,這是導致 this 含義如此豐富的主要原因。一個函式被執行時,會建立一個執行環境(ExecutionContext),函式的所有行為均發生於此。建構該執行環境時,JavaScript 首先會建立 arguments 變數,其中包含呼叫時傳入的參數;接著建立作用域鏈;然後初始化變數,先初始化形參表,值取自 arguments,若無對應值則初始化為 undefined;若函式含內部函式,則初始化這些內部函式;最後為 this 變數賦值,根據呼叫方式的不同,賦給全域物件、當前物件等。至此執行環境建構完成,函式開始逐行執行。」
理解這段流程,再回頭看前面那些看似矛盾的範例,一切就都說得通了。