| 沙沙起 |
|
||
|
本文主要解釋在JS里面this關鍵字的指向問題(在瀏覽器環境下)。 閱讀此文章,還需要心平氣和的閱讀完,相信一定會有所收獲,我也會不定期的發布,分享一些文章,共同學習 首先,必須搞清楚在JS里面,函數的幾種調用方式:
但是不管函數是按哪種方法來調用的,請記住一點:誰調用這個函數或方法,this關鍵字就指向誰。 接下來就分情況來討論下這些不同的情況: 普通函數調用1 function person(){ 2 this.name="xl"; 3 console.log(this); 4 console.log(this.name); 5 } 6 7 person(); //輸出 window xl 8
在這段代碼中 1 var name="xl"; 2 function person(){ 3 console.log(this.name); 4 } 5 person(); //輸出 xl
同樣這個地方 作為方法來調用在上面的代碼中,普通函數的調用即是作為 再來看下其他的形式 var name="XL"; var person={ name:"xl", showName:function(){ console.log(this.name); } } person.showName(); //輸出 xl //這里是person對象調用showName方法,很顯然this關鍵字是指向person對象的,所以會輸出name var showNameA=person.showName; showNameA(); //輸出 XL //這里將person.showName方法賦給showNameA變量,此時showNameA變量相當于window對象的一個屬性,因此showNameA()執行的時候相當于window.showNameA(),即window對象調用showNameA這個方法,所以this關鍵字指向window
再換種形式: var personA={ name:"xl", showName:function(){ console.log(this.name); } } var personB={ name:"XL", sayName:personA.showName } personB.sayName(); //輸出 XL //雖然showName方法是在personA這個對象中定義,但是調用的時候卻是在personB這個對象中調用,因此this對象指向
作為構造函數來調用1 function Person(name){ 2 this.name=name; 3 } 4 var personA=Person("xl"); 5 console.log(personA.name); // 輸出 undefined 6 console.log(window.name);//輸出 xl 7 //上面代碼沒有進行new操作,相當于window對象調用Person("xl")方法,那么this指向window對象,并進行賦值操作window.name="xl". 8 9 var personB=new Person("xl"); 10 console.log(personB.name);// 輸出 xl 11 //這部分代碼的解釋見下
new操作符//下面這段代碼模擬了new操作符(實例化對象)的內部過程 function person(name){ var o={}; o.__proto__=Person.prototype; //原型繼承 Person.call(o,name); return o; } var personB=person("xl"); console.log(personB.name); // 輸出 xl 這段代碼涉及到了_proto_及prototype的概念,如果有需要了解,請點擊鏈接
call/apply方法的調用在JS里函數也是對象,因此函數也有方法。從Function.prototype上繼承到
1 var name="XL"; 2 var Person={ 3 name:"xl", 4 showName:function(){ 5 console.log(this.name); 6 } 7 } 8 Person.showName.call(); //輸出 "XL" 9 //這里call方法里面的第一個參數為空,默認指向window。 10 //雖然showName方法定義在Person對象里面,但是使用call方法后,將showName方法里面的this指向了window。因此最后會輸出"XL"; 11 funtion FruitA(n1,n2){ 12 this.n1=n1; 13 this.n2=n2; 14 this.change=function(x,y){ 15 this.n1=x; 16 this.n2=y; 17 } 18 } 19 20 var fruitA=new FruitA("cheery","banana"); 21 var FruitB={ 22 n1:"apple", 23 n2:"orange" 24 }; 25 fruitA.change.call(FruitB,"pear","peach"); 26 27 console.log(FruitB.n1); //輸出 pear 28 console.log(FruitB.n2);// 輸出 peach
Function.prototype.bind()方法var name="XL"; function Person(name){ this.name=name; this.sayName=function(){ setTimeout(function(){ console.log("my name is "+this.name); },50) } } var person=new Person("xl"); person.sayName() //輸出 “my name is XL”; //這里的setTimeout()定時函數,相當于window.setTimeout(),由window這個全局對象對調用,因此this的指向為window, 則this.name則為XL
那么如何才能輸出 var name="XL"; function Person(name){ this.name=name; this.sayName=function(){ setTimeout(function(){ console.log("my name is "+this.name); }.bind(this),50) //注意這個地方使用的bind()方法,綁定setTimeout里面的匿名函數的this一直指向Person對象 } } var person=new Person("xl"); person.sayName(); //輸出 “my name is xl”;
這里 另外幾個需要注意的地方: var name="XL"; function Person(){ this.name="xl"; this.showName=function(){ console.log(this.name); } setTimeout(this.showName,50); } var person=new Person(); //輸出 "XL" //在setTimeout(this.showName,50)語句中,會延時執行this.showName方法 //this.showName方法即構造函數Person()里面定義的方法。50ms后,執行this.showName方法,this.showName里面的this此時便指向了window對象。則會輸出"XL";
修改上面的代碼: 1 var name="XL"; 2 function Person(){ 3 this.name="xl"; 4 var that=this; 5 this.showName=function(){ 6 console.log(that.name); 7 } 8 setTimeout(this.showName,50) 9 } 10 var person=new Person(); //輸出 "xl" 11 12 13 14 //這里在Person函數當中將this賦值給that,即讓that保存Person對象,因此在setTimeout(this.showName,50)執行過程當中,console.log(that.name)即會輸出Person對象的屬性"xl"
匿名函數: 1 var name="XL"; 2 var person={ 3 name:"xl", 4 showName:function(){ 5 console.log(this.name); 6 } 7 sayName:function(){ 8 (function(callback){ 9 callback(); 10 })(this.showName) 11 } 12 } 13 person.sayName(); //輸出 XL 14 var name="XL"; 15 var person={ 16 name:"xl", 17 showName:function(){ 18 console.log(this.name); 19 } 20 sayName:function(){ 21 var that=this; 22 (function(callback){ 23 callback(); 24 })(that.showName) 25 } 26 } 27 person.sayName() ; //輸出 "xl" 28 //匿名函數的執行同樣在默認情況下this是指向window的,除非手動改變this的綁定對象
Eval函數該函數執行的時候,this綁定到當前作用域的對象上 var name="XL"; var person={ name:"xl", showName:function(){ eval("console.log(this.name)"); } } person.showName(); //輸出 "xl" var a=person.showName; a(); //輸出 "XL"
箭頭函數
function Timer() { this.seconds = 0; setInterval( () => this.seconds ++, 1000); } var timer = new Timer(); setTimeout( () => console.log(timer.seconds), 3100); // 3 // 在構造函數內部的setInterval()內的回調函數,this始終指向實例化的對象,并獲取實例化對象的seconds的屬性,每1s這個屬性的值都會增加1。否則最后在3s后執行setTimeOut()函數執行后輸出的是0 參考資料 |
![]() |
|
|
博客園
|
|