this
this 是 JavaScript 的一個關鍵字,隨著函數使用場合不同,this 的值會發生變化。但總有一個原則:this 指的是調用函數的那個對象。
一、定義
1、this 是函數內部的一個特殊對象(或 this 引用),它引用的是函數據以執行的環境對象。
2、this 引用是一種在 JavaScript 代碼中隨時可用的只讀變量,它引用(指向)一個對象,且會根據代碼上下文自動改變其引用對象。規則如下:
• 在最外層代碼中,this 引用全局對象;
• 在函數內,this 引用取決於函數調用方式:1)構造函數調用——this 引用所生成的對象;2)方法調用——this 引用接收方對象;3)apply 或 call 調用——this 引用由參數指定的對象;4)其他方式調用——this 引用全局對象。
二、this 對象(引用)的使用情況總結
JavaScript 是動態語言,this 關鍵字在執行時才能確定是誰,所以 this 永遠指向調用者,即對「調用對象」的引用。簡單說,調用的方法屬於哪個對象,this 就指向那個對象。根據調用方式不同,this 可指向全局對象、當前對象或其他任意對象。
1、全局函數調用,全局函數中的 this 指向全局對象 window(函數調用模式)。
//代碼清單1 <script type="text/javascript"> var message = "this in window"; //這一句寫在函數外面和裡面是一樣效果 function func() { if(this == window){ alert("this == window"); alert(message); this.methodA = function() { alert("I'm a function"); } } } func(); //如果不調用func方法,則裡面定義的屬性或方法會取不到 methodA(); </script>
func() 的調用結果為 this == window、this in window;methodA() 的調用結果為 I'm a function。
2、構造函數調用,即用 new 實例化一個對象,this 指向通過構造函數生成的對象(構造器調用模式)。
<script type="text/javascript"> function Func() { if (this == window) { alert("this == window"); } else { alert("this != window"); } this.fieldA = "I'm a field"; alert(this); } var obj = new Func(); alert(obj.fieldA); //this指向的是對象obj </script>
3、對象方法的調用,this 指向當前對象。任何函數只要被當作對象的方法使用或賦值,其內部 this 都是對該對象本身的引用(方法調用模式)。
//代碼清單3 <script type="text/javascript"> var obj = { x: 3, doit: function(){ if(this == window){ alert("this == window"); }else{ alert("method is called: " + this.x); } } }; obj.doit(); //this指向的是對象obj </script>
4、通過 apply 或 call 調用,this 指向傳入的對象。call 方法可將函數的對象上下文從初始上下文改為 thisObj 指定的新對象,若未提供 thisObj,則 Global 對象被用作 thisObj(apply 調用模式)。
//代碼清單4 <script type="text/javascript"> var obj = { x: 3, doit: function(){ alert("method is called: " + this.x); } }; var obj2 = {x: 4}; obj.doit(); //3,this指向obj obj.doit.apply(obj2); //4,this指向obj2 obj.doit.call(obj2); //4,this指向obj2 </script>
5、原型鏈中的 this——原型對象及構造函數中的 this 指向新創建的實例對象。使用 prototype 擴展方法時可用 this 獲取源對象的實例,但私有字段無法通過原型鏈獲取。
//代碼清單5 <script type="text/javascript"> function Func() { this.fieldA = "I'm a field"; var privateFieldA = "I'm a var"; } Func.prototype = { ExtendMethod: function(str) { alert(str + " :" + this.fieldA); alert(privateFieldA); //出錯,私有字段無法通過原型鏈獲取。 } }; var obj = new Func(); obj.ExtendMethod("From prototype"); //此時構造函數及原型鏈中的this指向對象obj </script>
6、閉包中的 this——寫在 function 中的 function,this 指向全局對象 window。
6.1 對象中的閉包
//代碼清單6 <script type="text/javascript"> var name = "The window"; var obj = { name: "My Object", getNameFunc: function(){ return function(){ return this.name; } } }; alert(obj.getNameFunc()()); //The window </script>
此時閉包中的 this 指向全局對象 window,只能取到全局對象的屬性。若想訪問對象內部屬性(外部函數的變量),可把外部函數的 this 保存在一個閉包能訪問的變量裡:
//代碼清單7 <script type="text/javascript"> var name = "The window"; var obj = { name: "My Object", getNameFunc: function(){ var that = this; return function(){ return that.name; } } }; alert(obj.getNameFunc()()); //My object </script>
將外部函數的 this 賦值給 that 變量,就能讀取到外部函數的變量。
6.2 不管是直接引用 function 還是實例化一個 function,其返回的閉包函數裡的 this 都指向 window。
//代碼清單8 <script type="text/javascript"> function a() { alert(this == window); var that = this; var func = function() { alert(this == window); alert(that); }; return func; } var b = a(); b(); //true, true, [object Window] var c = new a(); c(); //false, true, [object object] </script>
7、函數使用 bind() 方法綁定一個對象,this 會指向傳給 bind() 的值。
//代碼清單9 <script type="text/javascript"> window.color = "red"; var obj = {color: "blue"}; function sayColor(){ alert(this.color); } var objSayColor = sayColor.bind(obj); objSayColor(); //blue </script>
8、內嵌在 HTML 元素中的腳本段,this 指向元素本身。
//代碼清單10 <div onclick="test(this)" id="div">Click Me</div> <script type="text/javascript"> function test(obj) { alert(obj); //[object HTMLDivElement] } </script>
9、寫在 script 標簽中:this 就是全局對象 window,與第一點的全局函數調用同理。
以上就是 JavaScript 中的 this 引用,希望對大家有所幫助。