最近又遇到了JacvaScript中的call()方法和apply()方法,而在某些時候這兩個方法還確實是十分重要的,那么就讓我總結這兩個方法的使用和區別吧。
1.改變函數內部的this指向的三種方法:call(),apply(),bind()
2. 相同點:都可以改變this指向。
都是在特定的作用域中調用函數,等於設置函數體內this對象的值,以擴充函數賴以運行的作用域。
一般來說,this總是指向調用某個方法的對象,但是使用call()和apply()方法時,就會改變this的指向。
call()方法使用示例:
語法: 函數.call(this, arg1, arg2, arg3, arg4)
第一個參數用來指定函數內部的this指向,后面的參數是函數執行時所需的實參。
//例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()方法使用示例:
語法: 函數.apply(this, []);
第一個參數用來指定函數內部的this指向;
第二個參數要求是一個數組或者偽數組,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();
bind()方法使用示例:
語法: 函數.bind(this);
該方法返回一個函數,是一個新的函數,
this.name="jack"; var demo={ name:"rose", getName:function(){return this.name;} } console.log(demo.getName());//輸出rose 這里的this指向demo var another=demo.getName; console.log(another())//輸出jack 這里的this指向全局對象 //運用bind方法更改this指向 var another2=another.bind(demo); console.log(another2());//輸出rose 這里this指向了demo對象了;
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。
注:不同之處是call和apply是一次性改變this,bind是永久性改變this,可以認為bind的靈活性更高,你可以先把this綁定好, 然后什么時候想執行就什么時候執行。
使用示例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>