JavaScript&jQuery.this關鍵字

來源:互聯網
上載者:User

標籤:char   val   doc   obj   plain   位置   on()   AC   key   

this關鍵字

this關鍵字通常在函數內部或對象內部使用。

函數或方法聲明的位置不同,會影響this關鍵字的含義。

通常來說,this指向當前函數所操作的對象。

<!DOCTYPE html>

< html lang = "en" > < head >      < meta charset = "UTF-8" >      < title >this關鍵字</ title > </ head > < body > < script >      window.onload=function (ev) {          // 調用對象          this.person.eat();      }       var person={};//字面量建立對象       // 設定字面量對象屬性      person.name=‘huangshiren‘;      person.age=58;      person.appetite=3;      person.eat=function(){          var value=this.name+‘的飯量是‘+this.appetite;          document.write(value+‘< br >‘);          document.write(‘正在吃飯‘);      } </ script > </ body > </ html >測試題目

1、如何判斷this關鍵字的指向?

答:

  • fun()是全域函數,而聲明的fun接收的是obj裡面單純的一個函數,並沒有調用(obj.say()才是調用了函數),此時的fun就是一個函數(function(){alert(this.str);}),那麼當fun()調用函數的時候,this指向的就是window
  • 是誰調用的函數,那麼this就指向誰

2、不使用this關鍵字可以嗎?

答:不可以

 

JavaScript&jQuery.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.