JavaScript之this指標深入詳解

來源:互聯網
上載者:User

JavaScript之this指標深入詳解

  

javascript中的this含義非常豐富,它可以是全域對象,當前對象或者是任意對象,這都取決於函數的調用方式。函數有以下幾種調用方式:作為對象方法調用、作為函數調用、作為建構函式調用、apply或call調用。

對象方法調用

作為對象方法調用的時候,this會被綁定到該對象。

 
  1. var point = { 
  2. x : 0, 
  3. y : 0, 
  4. moveTo : function(x, y) { 
  5.      this.x = this.x + x; 
  6.      this.y = this.y + y; 
  7.      } 
  8. }; 

point.moveTo(1, 1)//this 綁定到當前對象,即 point 對象

這裡我想強調一點內容,就是this是在函數執行的時候去擷取對應的值,而不是函數定義時。即使是對象方法調用,如果該方法的函數屬性以函數名的形式傳入別的範圍,也會改變this的指向。我舉一個例子:

 
  1. var a = { 
  2. aa : 0, 
  3. bb : 0, 
  4. fun : function(x,y){ 
  5.   this.aa = this.aa + x; 
  6.   this.bb = this.bb + y; 
  7. }; 
  8. var aa = 1; 
  9. var b = { 
  10. aa:0, 
  11. bb:0, 
  12. fun : function(){return this.aa;} 
  13. a.fun(3,2); 
  14. document.write(a.aa);//3,this指向對象本身 
  15. document.write(b.fun());//0,this指向對象本身 
  16. (function(aa){//注意傳入的是函數,而不是函數執行的結果 
  17. var c = aa(); 
  18. document.write(c);//1 , 由於fun在該處執行,導致this不再指向對象本身,而是這裡的window 
  19. })(b.fun); 

這樣就明白了吧。這是一個容易混淆的地方。

函數調用

函數也可以直接被調用,這個時候this被綁定到了全域對象。

 
  1. var x = 1; 
  2.  function test(){ 
  3.  this.x = 0; 
  4.  } 
  5.  test(); 
  6.  alert(x); //0 

但這樣就會出現一些問題,就是在函數內部定義的函數,其this也會指向全域,而和我們希望的恰恰相反。代碼如下:

 
  1. var point = { 
  2. x : 0, 
  3. y : 0, 
  4. moveTo : function(x, y) { 
  5.      // 內建函式 
  6.      var moveX = function(x) { 
  7.      this.x = x;//this 綁定到了全域 
  8.     }; 
  9.     // 內建函式 
  10.     var moveY = function(y) { 
  11.     this.y = y;//this 綁定到了全域 
  12.     }; 
  13.  
  14.     moveX(x); 
  15.     moveY(y); 
  16.     } 
  17. }; 
  18. point.moveTo(1, 1); 
  19. point.x; //==>0 
  20. point.y; //==>0 
  21. x; //==>1 
  22. y; //==>1 

我們會發現不但我們希望的移動呢效果沒有完成,反而會多出兩個全域變數。那麼如何解決呢?只要要進入函數中的函數時將this儲存到一個變數中,再運用該變數即可。代碼如下:

 
  1. var point = { 
  2. x : 0, 
  3. y : 0, 
  4. moveTo : function(x, y) { 
  5.       var that = this; 
  6.      // 內建函式 
  7.      var moveX = function(x) { 
  8.      that.x = x; 
  9.      }; 
  10.      // 內建函式 
  11.      var moveY = function(y) { 
  12.      that.y = y; 
  13.      } 
  14.      moveX(x); 
  15.      moveY(y); 
  16.      } 
  17. }; 
  18. point.moveTo(1, 1); 
  19. point.x; //==>1 
  20. point.y; //==>1 

建構函式調用

在javascript中自己建立建構函式時可以利用this來指向新建立的對象上。這樣就可以避免函數中的this指向全域了。

 
  1. var x = 2; 
  2. function test(){ 
  3. this.x = 1; 
  4. var o = new test(); 
  5. alert(x); //2 

apply或call調用

這兩個方法可以切換函數執行的上下文環境,也就是改變this綁定的對象。apply和call比較類似,區別在於傳入參數時一個要求是數組,一個要求是分開傳入。所以我們以apply為例:

 
  1. <pre name="code" class="html">var name = "window"; 
  2. var someone = { 
  3.     name: "Bob", 
  4.     showName: function(){ 
  5.         alert(this.name); 
  6.     } 
  7. }; 
  8.  
  9. var other = { 
  10.     name: "Tom" 
  11. };   
  12.  
  13. someone.showName();  //Bob 
  14. someone.showName.apply();    //window 
  15. someone.showName.apply(other);    //Tom 

可以看到,正常訪問對象中方法時,this指向對象。使用了apply後,apply無參數時,this的當前對象是全域,apply有參數時,this的當前對象就是該參數。

箭頭函數調用

這裡需要補充一點內容,就是在下一代javascript標準ES6中的箭頭函數的 this始終指向函數定義時的 this,而非執行時。我們通過一個例子來理解:

 
  1. var o = { 
  2.     x : 1, 
  3.     func : function() { console.log(this.x) }, 
  4.     test : function() { 
  5.         setTimeout(function() { 
  6.             this.func(); 
  7.         }, 100); 
  8.     } 
  9. }; 
  10.  
  11. o.test(); // TypeError : this.func is not a function 

上面的代碼會出現錯誤,因為this的指向從o變為了全域。我們需要修改上面的代碼如下:

 
  1. var o = { 
  2.     x : 1, 
  3.     func : function() { console.log(this.x) }, 
  4.     test : function() { 
  5.         var _this = this; 
  6.         setTimeout(function() { 
  7.             _this.func(); 
  8.         }, 100); 
  9.     } 
  10. }; 
  11.  
  12. o.test(); 

通過使用外部事先儲存的this就行了。這裡就可以利用到箭頭函數了,我們剛才說過,箭頭函數的 this始終指向函數定義時的 this,而非執行時。所以我們將上面的代碼修改如下:

 
  1. var o = { 
  2.     x : 1, 
  3.     func : function() { console.log(this.x) }, 
  4.     test : function() { 
  5.         setTimeout(() => { this.func() }, 100); 
  6.     } 
  7. }; 
  8.  
  9. o.test(); 

這回this就指向o了,我們還需要注意一點的就是這個this是不會改變指向對象的,我們知道call和apply可以改變this的指向,但是在箭頭函數中是無效的。

 
  1. var x = 1, 
  2.     o = { 
  3.         x : 10, 
  4.         test : () => this.x 
  5.     }; 
  6.  
  7. o.test(); // 1 
  8. o.test.call(o); // 依然是1 

這樣就可以明白各種情況下this綁定對象的區別了。


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.