JavaScript中的this對象知識

來源:互聯網
上載者:User

JavaScript中的this總是讓人迷惑,應該是js眾所周知的坑之一。 個人也覺得js中的this不是一個好的設計,由於this晚綁定的特性,它可以是全域對象,當前對象,或者…有人甚至因為坑大而不用this。其實如果完全掌握了this的工作原理,自然就不會走進這些坑。來看下以下這些情況中的this分別會指向什麼:

1.全域代碼中的this

alert(this)//window

全域範圍內的this將會指向全域對象,在瀏覽器中即使window。

2.作為單純的函數調用

functionfooCoder(x) {    this.x = x;}fooCoder(2);alert(x);// 全域變數x值為2

這裡this指向了全域對象,即window。在strict 模式中,則是undefined。

3.作為對象的方法調用

varname = "clever coder";varperson = {    name : "foocoder",    hello : function(sth){        console.log(this.name + " says "+ sth);    }}person.hello("hello world");

輸出 foocoder says hello world。this指向person對象,即當前對象。

4.作為建構函式

newFooCoder();

函數內部的this指向新建立的對象。

5.內建函式

varname = "clever coder";varperson = {    name : "foocoder",    hello : function(sth){        varsayhello = function(sth) {            console.log(this.name + " says "+ sth);        };        sayhello(sth);    }}person.hello("hello world");//clever coder says hello world

在內建函式中,this沒有按預想的綁定到外層函數對象上,而是綁定到了全域對象。這裡普遍被認為是JavaScript語言的設計錯誤,因為沒有人想讓內建函式中的this指向全域對象。一般的處理方式是將this作為變數儲存下來,一般約定為that或者self:

varname = "clever coder";varperson = {    name : "foocoder",    hello : function(sth){        varthat = this;        varsayhello = function(sth) {            console.log(that.name + " says "+ sth);        };        sayhello(sth);    }}person.hello("hello world");//foocoder says hello world 

6.使用call和apply設定this

person.hello.call(person, "world");

apply和call類似,只是後面的參數是通過一個數組傳入,而不是分開傳入。兩者的方法定義:

call( thisArg [,arg1,arg2,… ] );  // 參數列表,arg1,arg2,...apply(thisArg [,argArray] );     // 參數數組,argArray

兩者都是將某個函數綁定到某個具體對象上使用,自然此時的this會被顯式的設定為第一個參數。

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.