轉載:JS call()方法和apply()方法

來源:互聯網
上載者:User

標籤:first   tle   lan   替換   tar   mod   ring   param   new   

原文:http://blog.csdn.net/ganyingxie123456/article/details/70855586

這個是講的比較簡單的,也是最常用的吧。

-------------------------------------------------------------------

 

1. 每個函數都包含兩個非繼承而來的方法:call()方法和apply()方法。 2. 相同點:這兩個方法的作用是一樣的。

都是在特定的範圍中調用函數,等於設定函數體內this對象的值,以擴充函數賴以啟動並執行範圍。

一般來說,this總是指向調用某個方法的對象,但是使用call()和apply()方法時,就會改變this的指向。

call()方法使用樣本:

    //例1    <script>        window.color = ‘red‘;        document.color = ‘yellow‘;        var s1 = {color: ‘blue‘ };        function changeColor(){            console.log(this.color);        }        changeColor.call();         //red (預設傳遞參數)        changeColor.call(window);   //red        changeColor.call(document); //yellow        changeColor.call(this);     //red        changeColor.call(s1);       //blue    </script>    //例2    var Pet = {        words : ‘...‘,        speak : function (say) {            console.log(say + ‘‘+ this.words)        }    }    Pet.speak(‘Speak‘); // 結果:Speak...    var Dog = {        words:‘Wang‘    }    //將this的指向改變成了Dog    Pet.speak.call(Dog, ‘Speak‘); //結果: SpeakWang

 

 

apply()方法使用樣本:

    //例1    <script>        window.number = ‘one‘;        document.number = ‘two‘;        var s1 = {number: ‘three‘ };        function changeColor(){            console.log(this.number);        }        changeColor.apply();         //one (預設傳參)        changeColor.apply(window);   //one        changeColor.apply(document); //two        changeColor.apply(this);     //one        changeColor.apply(s1);       //three    </script>    //例2    function Pet(words){        this.words = words;        this.speak = function () {            console.log( this.words)        }    }    function Dog(words){        //Pet.call(this, words); //結果: Wang       Pet.apply(this, arguments); //結果: Wang    }    var dog = new Dog(‘Wang‘);    dog.speak();

 

 

3. 不同點:接收參數的方式不同。
  • apply()方法 接收兩個參數,一個是函數啟動並執行範圍(this),另一個是參數數組。

文法:apply([thisObj [,argArray] ]);,調用一個對象的一個方法,2另一個對象替換當前對象。

說明:如果argArray不是一個有效數組或不是arguments對象,那麼將導致一個 
TypeError,如果沒有提供argArray和thisObj任何一個參數,那麼Global對象將用作thisObj。

  • call()方法 第一個參數和apply()方法的一樣,但是傳遞給函數的參數必須列舉出來。

文法:call([thisObject[,arg1 [,arg2 [,...,argn]]]]);,應用某一對象的一個方法,用另一個對象替換當前對象。

說明: call方法可以用來代替另一個對象調用一個方法,call方法可以將一個函數的物件內容從初始的上下文改變為thisObj指定的新對象,如果沒有提供thisObj參數,那麼Global對象被用於thisObj。

使用樣本1:

    function add(c,d){        return this.a + this.b + c + d;    }    var s = {a:1, b:2};    console.log(add.call(s,3,4)); // 1+2+3+4 = 10    console.log(add.apply(s,[5,6])); // 1+2+5+6 = 14 

 

 

使用樣本2:

    <script>        window.firstName = "Cynthia";         window.lastName = "_xie";        var myObject = {firstName:‘my‘, lastName:‘Object‘};        function getName(){            console.log(this.firstName + this.lastName);        }        function getMessage(sex,age){            console.log(this.firstName + this.lastName + " 性別: " + sex + " age: " + age );        }        getName.call(window); // Cynthia_xie        getName.call(myObject); // myObject        getName.apply(window); // Cynthia_xie        getName.apply(myObject);// myObject        getMessage.call(window,"女",21); //Cynthia_xie 性別: 女 age: 21        getMessage.apply(window,["女",21]); // Cynthia_xie 性別: 女 age: 21        getMessage.call(myObject,"未知",22); //myObject 性別: 未知 age: 22        getMessage.apply(myObject,["未知",22]); // myObject 性別: 未知 age: 22    </script>

 

 

轉載:JS call()方法和apply()方法

相關文章

聯繫我們

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