Java程式員的JavaScript學習筆記(3——this/call/apply)

來源:互聯網
上載者:User

Java程式員的JavaScript學習筆記(3——this/call/apply)
計劃按如下順序完成這篇筆記:

    理念。屬性複製和繼承。this/call/apply。閉包/getter/setter。prototype。物件導向類比。jQuery基本機制。jQuery選取器。jQuery工具方法。jQuery-在“類”層面擴充。jQuery-在“對象”層面擴充。jQuery-延伸選取器。jQuery UI。擴充jQuery UI。

    這是筆記的第3篇,聊聊JavaScript中的this,還有兩種調用函數的特殊方式:call 和 apply。

    this

    JavaScript中的this和Java中的this區別較大。學過Java的童鞋請忘掉你知道的關於this的一切。

    JavaScript中的this是依賴於函數的,表示函數的調用者。有兩個意思:第一,this不是函數本身;第二,函數被調用的時候才決定this指向誰。

    分以下幾種情況:

    調用全域函數

    看下面的代碼:

    程式碼片段1:function UiObject(){    console.log(this.name || this.toString()); // 輸出:[object Window]    console.log(this===window); // 輸出:true}UiObject()
    預設為window調用,this為window。

    通過對象調用

    程式碼片段2:function UiObject(){this.render = function(str){console.log('render this ',str,' by ' + this.toString());console.log(this === UiObject);  // falseconsole.log(this === ui); // true}}var ui = new UiObject();ui.render();

    如果函數在原型中定義呢?

    程式碼片段3:function UiObject(){}UiObject.prototype = {render : function(str){console.log('render this ',str,' by ' + this.toString());console.log(this === UiObject);  // falseconsole.log(this === ui);   // true ,結果一樣,誰調用,this指向誰}};var ui = new UiObject();ui.render();
    結果一樣,在哪兒定義不重要,被誰調用才重要。有奶的就是娘,誰生的不重要,好現實...
    再寫段代碼讓這個特性更明顯,如下:

    程式碼片段4:        function hunt(){console.log('To ',this.name + ', my dear lord! I will hunt ',this.target,' as your wish.');console.log(this === hailong);console.log(this === haichao);}var hailong = {name : 'Lord Hailong',target : 'deer'};var haichao = {name : 'Lord Haichao',target : ' Little-Japs '};hailong.hunt = hunt;// 輸出:To Lord Hailong, my dear lord! I will hunt deer as your wish.// 輸出:true   --  console.log(this === hailong);// 輸出:false  --  console.log(this === haichao);hailong.hunt();haichao.kill = hunt;// 輸出:To Lord Haichao, my dear lord! I will hunt Little-Japs as your wish.// 輸出:false  --  console.log(this === hailong);// 輸出:true   --  console.log(this === haichao);haichao.kill();
    有奶就是娘,誰調用,this就指向誰。

    被事件調用

    被事件調用分幾種情況:

    1、

    2、oDiv.onclick = theFunc ;

    第1中情況,this指向window,第2中情況,this指向oDiv。


    arguments

    和this類似,函數被調用時,argmuments將被賦值為傳入的傳入的參數數組。

    可以通過 arguments.length獲得傳入參數的個數,可以通過arguments[idx]獲得傳入參數的值。


    基於this的特性,我們需要特殊的方法靈活控制this的指向。

    JavaScript為我們提供了call和apply兩個方法。
    call

    在函數調用的時候,通過call方法,可以指定調用者,從而實現了對this的靈活綁定。文法如下:

    func.call(invokerObject);

    func是函數,invokerObject將被指定為函數的調用者。看下面的例子。

    程式碼片段5:
    function hunt(){}var hailong = {name : 'Lord Hailong',target : 'deer'};var haichao = {name : 'Lord Haichao',target : ' Little-Japs '};function printName(){console.log('Name:',this.name);}printName.call(hunt);printName.call(hailong);printName.call(haichao);

    定義好printName方法後,希望能應用到hunt、hailong、haichao幾個對象上。通過call函數即可實現。printName函數中的this成功指向了通過call參數傳入的對象。

    printName.call(hailong)

    等同於下面的語句:

    hailong.printName = printName;

    hailong.printName();

    delete haiong.printName;


    apply

    通過call方法調用函數,如果有參數,從call方法的第二個參數開始傳入。文法如下:

    function func(p1,p2,p3){}

    func.call(targetObject,p1,p2,p3)

    apply方法與call方法實現相同的功能,不同的是,當傳入參數時,所有的參數組成一個數組,作為apply的第二個參數傳入,文法如下:

    func.apply(targetObject,[p1,p2,p3])

    同arguments結合使用時,apply方法更方便。


    小結搞清楚this的含義,是基礎中的基礎。通過call、apply可以靈活綁定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.