DIV CSS 佈局教程網

JavaScript 運行機制中的 this 詳解
編輯:JavaScript基礎知識     

this 是物件導向語言中重要的關鍵字,理解並掌握它對代碼的健壯性與優美性至關重要。而 JavaScript 的 this 又有別於 Java、C# 等純物件導向語言,因此更顯撲朔迷離。

this 常見於以下六種情境:

  1. 純函數
  2. 物件方法呼叫
  3. 使用 new 呼叫構造函數
  4. 內部函數
  5. 使用 call / apply
  6. 事件綁定

1. 純函數

var name = 'this is window';   // 定義 window 的 name 屬性
function getName(){
    console.log(this);         // 輸出 Window,this 指向全域物件 window
    console.log(this.name);    // 輸出 "this is window"
}
getName();

純函數中的 this 指向全域物件 window。

2. 物件方法呼叫

var name = 'this is window';
var testObj = {
    name : 'this is testObj',
    getName : function(){
        console.log(this);      // 輸出 testObj
        console.log(this.name); // 輸出 "this is testObj"
    }
}
testObj.getName();

被呼叫方法中 this 指向呼叫該方法的物件。

3. 使用 new 呼叫構造函數

function getObj(){
    console.log(this); // 輸出 getObj{},指向新建立的 getObj 物件
}
new getObj();

new 構造函數中的 this 指向新生成的物件。

4. 內部函數

var name = "this is window";
var testObj = {
    name : "this is testObj",
    getName : function(){
        // var self = this;    // 臨時保存 this
        var handle = function(){
            console.log(this);      // 輸出 Window,指向全域物件
            console.log(this.name); // 輸出 "this is window"
            // console.log(self);   // 這樣才能取到指向 testObj 的 this
        }
        handle();
    }
}
testObj.getName();

內部函數中的 this 仍指向全域物件 window。這普遍被認為是 JS 的設計錯誤,因為沒人想讓內部函數的 this 指向全域物件。一般做法是將 this 存為變數(慣例命名為 that 或 self)。

5. 使用 call / apply

var name = 'this is window';
var testObj1 = {
    name : 'this is testObj1',
    getName : function(){
        console.log(this);      // 輸出 testObj2
        console.log(this.name); // 輸出 "this is testObj2"
    }
}
var testObj2 = {
    name: 'this is testObj2'
}
testObj1.getName.apply(testObj2);
testObj1.getName.call(testObj2);

apply 與 call 類似,只是第二個參數不同:call 使用參數列表 arg1, arg2...;apply 使用參數陣列 argArray。使用 call/apply 的函數,其 this 指向綁定的物件。

6. 事件綁定

事件方法中的 this 最容易讓人困惑,多數出錯都源於此。

// 頁面 Element 上綁定
<script type="text/javascript">
    function btClick(){
        console.log(this); // 輸出 Window,指向全域物件
    }
</script>
<body>
    <button id="btn" onclick="btClick();" >點擊</button>
</body>
// js 中綁定方式(1)
<body>
    <button id="btn">點擊</button>
</body>
<script type="text/javascript">
    function btClick(){
        console.log(this); // 輸出 <button>,指向 Element 按鈕物件
    }
    document.getElementById("btn").onclick = btClick;
    document.getElementById("btn").onclick;
</script>
// js 中綁定方式(3)
<body>
    <button id="btn">點擊</button>
</body>
<script type="text/javascript">
    function btClick(){
        console.log(this);
    }
    document.getElementById("btn").addEventListener('click', btClick); // 輸出 <button>,指向 Element 按鈕物件
    document.getElementById("btn").attachEvent('onclick', btClick);    // IE 使用,輸出 Window,指向全域物件
</script>

總結:在頁面 Element 上直接綁定(onclick="btClick();")時 this 指向全域物件;而在 JS 中綁定時,除了 attachEvent 綁定的事件方法外,this 指向綁定事件的 Element 元素。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved