自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JavaScript之this指針深入詳解

開發(fā) 前端
javascript中的this含義非常豐富,它可以是全局對象,當(dāng)前對象或者是任意對象,這都取決于函數(shù)的調(diào)用方式。函數(shù)有以下幾種調(diào)用方式:作為對象方法調(diào)用、作為函數(shù)調(diào)用、作為構(gòu)造函數(shù)調(diào)用、apply或call調(diào)用。

javascript中的this含義非常豐富,它可以是全局對象,當(dāng)前對象或者是任意對象,這都取決于函數(shù)的調(diào)用方式。函數(shù)有以下幾種調(diào)用方式:作為對象方法調(diào)用、作為函數(shù)調(diào)用、作為構(gòu)造函數(shù)調(diào)用、apply或call調(diào)用。

[[160195]]

對象方法調(diào)用

作為對象方法調(diào)用的時(shí)候,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 綁定到當(dāng)前對象,即 point 對象

這里我想強(qiáng)調(diào)一點(diǎn)內(nèi)容,就是this是在函數(shù)執(zhí)行的時(shí)候去獲取對應(yīng)的值,而不是函數(shù)定義時(shí)。即使是對象方法調(diào)用,如果該方法的函數(shù)屬性以函數(shù)名的形式傳入別的作用域,也會改變this的指向。我舉一個(gè)例子:

  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指向?qū)ο蟊旧?/span> 
  15. document.write(b.fun());//0,this指向?qū)ο蟊旧?/span> 
  16. (function(aa){//注意傳入的是函數(shù),而不是函數(shù)執(zhí)行的結(jié)果 
  17. var c = aa(); 
  18. document.write(c);//1 , 由于fun在該處執(zhí)行,導(dǎo)致this不再指向?qū)ο蟊旧?,而是這里的window 
  19. })(b.fun); 

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

函數(shù)調(diào)用

函數(shù)也可以直接被調(diào)用,這個(gè)時(shí)候this被綁定到了全局對象。

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

但這樣就會出現(xiàn)一些問題,就是在函數(shù)內(nèi)部定義的函數(shù),其this也會指向全局,而和我們希望的恰恰相反。代碼如下:

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

我們會發(fā)現(xiàn)不但我們希望的移動呢效果沒有完成,反而會多出兩個(gè)全局變量。那么如何解決呢?只要要進(jìn)入函數(shù)中的函數(shù)時(shí)將this保存到一個(gè)變量中,再運(yùn)用該變量即可。代碼如下:

  1. var point = { 
  2. x : 0, 
  3. y : 0, 
  4. moveTo : function(x, y) { 
  5.       var that = this
  6.      // 內(nèi)部函數(shù) 
  7.      var moveX = function(x) { 
  8.      that.x = x; 
  9.      }; 
  10.      // 內(nèi)部函數(shù) 
  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 

構(gòu)造函數(shù)調(diào)用

在javascript中自己創(chuàng)建構(gòu)造函數(shù)時(shí)可以利用this來指向新創(chuàng)建的對象上。這樣就可以避免函數(shù)中的this指向全局了。

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

apply或call調(diào)用

這兩個(gè)方法可以切換函數(shù)執(zhí)行的上下文環(huán)境,也就是改變this綁定的對象。apply和call比較類似,區(qū)別在于傳入?yún)?shù)時(shí)一個(gè)要求是數(shù)組,一個(gè)要求是分開傳入。所以我們以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 

可以看到,正常訪問對象中方法時(shí),this指向?qū)ο?。使用了apply后,apply無參數(shù)時(shí),this的當(dāng)前對象是全局,apply有參數(shù)時(shí),this的當(dāng)前對象就是該參數(shù)。

箭頭函數(shù)調(diào)用

這里需要補(bǔ)充一點(diǎn)內(nèi)容,就是在下一代javascript標(biāo)準(zhǔn)ES6中的箭頭函數(shù)的 this始終指向函數(shù)定義時(shí)的 this,而非執(zhí)行時(shí)。我們通過一個(gè)例子來理解:

  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 

上面的代碼會出現(xiàn)錯誤,因?yàn)閠his的指向從o變?yōu)榱巳?。我們需要修改上面的代碼如下:

  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就行了。這里就可以利用到箭頭函數(shù)了,我們剛才說過,箭頭函數(shù)的 this始終指向函數(shù)定義時(shí)的 this,而非執(zhí)行時(shí)。所以我們將上面的代碼修改如下:

  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了,我們還需要注意一點(diǎn)的就是這個(gè)this是不會改變指向?qū)ο蟮模覀冎纁all和apply可以改變this的指向,但是在箭頭函數(shù)中是無效的。

  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綁定對象的區(qū)別了。

責(zé)任編輯:王雪燕 來源: brizer的博客
相關(guān)推薦

2021-02-17 11:25:33

前端JavaScriptthis

2011-07-15 01:38:56

C++this指針

2015-12-24 10:05:39

JavaScripttypeofinstanceof

2016-12-05 13:35:02

C語言數(shù)組指針

2017-04-07 11:15:49

原型鏈原型Javascript

2009-07-14 14:12:14

Javascript

2012-04-12 09:38:21

JavaScript

2021-12-21 15:31:10

C++語言指針

2017-11-14 14:41:11

Java泛型IO

2020-04-28 10:05:33

JavaScript繼承前端

2016-12-27 09:10:29

JavaScript原型鏈繼承

2017-08-08 09:15:41

前端JavaScript頁面渲染

2023-10-11 18:35:20

Java編程語言

2010-07-16 16:40:48

Perl引用

2009-12-18 15:24:52

2022-02-09 11:02:16

JavaScript前端框架

2011-06-03 13:48:18

JavaScript重構(gòu)

2021-10-17 22:40:51

JavaScript開發(fā) 框架

2024-01-25 11:42:00

C++編程指針常量

2017-05-11 21:01:20

JavaScript創(chuàng)建對象面向?qū)ο缶幊?/a>
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號