JS中的call、apply、bind方法詳解

call,apply

在 javascript 中,call 和 apply 都是為了改變某個(gè)函數(shù)運(yùn)行時(shí)的上下文(context)而存在的,換句話(huà)說(shuō),就是為了改變函數(shù)體內(nèi)部 this 的指向。JavaScript 的一大特點(diǎn)是,函數(shù)存在「定義時(shí)上下文」和「運(yùn)行時(shí)上下文」以及「上下文是可以改變的」這樣的概念

打個(gè)例子舉個(gè)比方:

function fruits() {}
 
fruits.prototype = {
    color: "red",
    say: function() {
        console.log("My color is " + this.color);
    }
}
 
var apple = new fruits;
apple.say();    //My color is red

但是如果我們有一個(gè)對(duì)象banana= {color : "yellow"} ,我們不想對(duì)它重新定義 say 方法,那么我們可以通過(guò) call 或 apply 用 apple 的 say 方法:

banana = {
    color: "yellow"
}
apple.say.call(banana);     //My color is yellow
apple.say.apply(banana);    //My color is yellow

所以,可以看出 call 和 apply 是為了動(dòng)態(tài)改變 this 而出現(xiàn)的,當(dāng)一個(gè) object 沒(méi)有某個(gè)方法(本栗子中banana沒(méi)有say方法),但是其他的有(本栗子中apple有say方法),我們可以借助call或apply用其它對(duì)象的方法來(lái)操作。


apply、call 區(qū)別

對(duì)于 apply、call 二者而言,作用完全一樣,只是接受參數(shù)的方式不太一樣。例如,有一個(gè)函數(shù)定義如下:

var func = function(arg1, arg2) {
     
};

就可以通過(guò)如下方式來(lái)調(diào)用:

func.call(this, arg1, arg2);
func.apply(this, [arg1, arg2])

其中 this 是你想指定的上下文,他可以是任何一個(gè) JavaScript 對(duì)象(JavaScript 中一切皆對(duì)象),call 需要把參數(shù)按順序傳遞進(jìn)去,而 apply 則是把參數(shù)放在數(shù)組里?! ?br> 為了鞏固加深記憶,下面列舉一些常用用法:


apply、call實(shí)例

數(shù)組之間追加
var array1 = [12 , "foo" , {name:"Joe"} , -2458]; 
var array2 = ["Doe" , 555 , 100]; 
Array.prototype.push.apply(array1, array2); 
// array1 值為  [12 , "foo" , {name:"Joe"} , -2458 , "Doe" , 555 , 100] 
獲取數(shù)組中的最大值和最小值
var  numbers = [5, 458 , 120 , -215 ]; 
var maxInNumbers = Math.max.apply(Math, numbers),   //458
    maxInNumbers = Math.max.call(Math,5, 458 , 120 , -215); //458

number 本身沒(méi)有 max 方法,但是 Math 有,我們就可以借助 call 或者 apply 使用其方法

驗(yàn)證是否是數(shù)組(前提是toString()方法沒(méi)有被重寫(xiě)過(guò))
functionisArray(obj){ 
    return Object.prototype.toString.call(obj) === '[object Array]' ;
}
類(lèi)(偽)數(shù)組使用數(shù)組方法
var domNodes = Array.prototype.slice.call(document.getElementsByTagName("*"));

Javascript中存在一種名為偽數(shù)組的對(duì)象結(jié)構(gòu)。比較特別的是 arguments 對(duì)象,還有像調(diào)用 getElementsByTagName , document.childNodes 之類(lèi)的,它們返回NodeList對(duì)象都屬于偽數(shù)組。不能應(yīng)用 Array下的 push , pop 等方法。
但是我們能通過(guò) Array.prototype.slice.call 轉(zhuǎn)換為真正的數(shù)組的帶有 length 屬性的對(duì)象,這樣 domNodes 就可以應(yīng)用 Array 下的所有方法了。


面試題

定義一個(gè) log 方法,讓它可以代理 console.log 方法,常見(jiàn)的解決方法是:

function log(msg) {
  console.log(msg);
}
log(1);    //1
log(1,2);    //1

上面方法可以解決最基本的需求,但是當(dāng)傳入?yún)?shù)的個(gè)數(shù)是不確定的時(shí)候,上面的方法就失效了,這個(gè)時(shí)候就可以考慮使用 apply 或者 call,注意這里傳入多少個(gè)參數(shù)是不確定的,所以使用apply是最好的,方法如下

function log(){
  console.log.apply(console, arguments);
};
log(1);    //1
log(1,2);    //1 2

接下來(lái)的要求是給每一個(gè) log 消息添加一個(gè)"(app)"的前輟,比如:

log("hello world"); //(app)hello world

該怎么做比較優(yōu)雅呢?這個(gè)時(shí)候需要想到arguments參數(shù)是個(gè)偽數(shù)組,通過(guò) Array.prototype.slice.call 轉(zhuǎn)化為標(biāo)準(zhǔn)數(shù)組,再使用數(shù)組方法unshift,像這樣:

function log(){
  var args = Array.prototype.slice.call(arguments);
  args.unshift('(app)');
 
  console.log.apply(console, args);
};


bind

在討論bind()方法之前我們先來(lái)看一道題目:
var altwrite = document.write;
altwrite("hello");

結(jié)果:Uncaught TypeError: Illegal invocation
altwrite()函數(shù)改變this的指向global或window對(duì)象,導(dǎo)致執(zhí)行時(shí)提示非法調(diào)用異常,正確的方案就是使用bind()方法:

altwrite.bind(document)("hello")

當(dāng)然也可以使用call()方法:

altwrite.call(document, "hello")
綁定函數(shù)

bind()最簡(jiǎn)單的用法是創(chuàng)建一個(gè)函數(shù),使這個(gè)函數(shù)不論怎么調(diào)用都有同樣的this值。常見(jiàn)的錯(cuò)誤就像上面的例子一樣,將方法從對(duì)象中拿出來(lái),然后調(diào)用,并且希望this指向原來(lái)的對(duì)象。如果不做特殊處理,一般會(huì)丟失原來(lái)的對(duì)象。使用bind()方法能夠很漂亮的解決這個(gè)問(wèn)題:

this.num = 9; 
var mymodule = {
  num: 81,
  getNum: function() { 
    console.log(this.num);
  }
};

mymodule.getNum(); // 81

var getNum = mymodule.getNum;
getNum(); // 9, 因?yàn)樵谶@個(gè)例子中,"this"指向全局對(duì)象

var boundGetNum = getNum.bind(mymodule);
boundGetNum(); // 81

bind() 方法與 apply 和 call 很相似,也是可以改變函數(shù)體內(nèi) this 的指向。

MDN的解釋是:bind()方法會(huì)創(chuàng)建一個(gè)新函數(shù),稱(chēng)為綁定函數(shù),當(dāng)調(diào)用這個(gè)綁定函數(shù)時(shí),綁定函數(shù)會(huì)以創(chuàng)建它時(shí)傳入 bind()方法的第一個(gè)參數(shù)作為 this,傳入 bind() 方法的第二個(gè)以及以后的參數(shù)加上綁定函數(shù)運(yùn)行時(shí)本身的參數(shù)按照順序作為原函數(shù)的參數(shù)來(lái)調(diào)用原函數(shù)。

舉個(gè)例子:

var bar = function(){
console.log(this.x);
}
var foo = {
x:3
}
bar(); // undefined
var func = bar.bind(foo);
func(); // 3

這里我們創(chuàng)建了一個(gè)新的函數(shù) func,當(dāng)使用 bind() 創(chuàng)建一個(gè)綁定函數(shù)之后,它被執(zhí)行的時(shí)候,它的 this 會(huì)被設(shè)置成 foo , 而不是像我們調(diào)用 bar() 時(shí)的全局作用域。


apply、call、bind比較

那么 apply、call、bind 三者相比較,之間又有什么異同呢?何時(shí)使用 apply、call,何時(shí)使用 bind 呢。簡(jiǎn)單的一個(gè)栗子:

var obj = {
    x: 81,
};
 
var foo = {
    getX: function() {
        return this.x;
    }
}
 
console.log(foo.getX.bind(obj)());  //81
console.log(foo.getX.call(obj));    //81
console.log(foo.getX.apply(obj));   //81

三個(gè)輸出的都是81,但是注意看使用 bind() 方法的,他后面多了對(duì)括號(hào)。

也就是說(shuō),區(qū)別是,當(dāng)你希望改變上下文環(huán)境之后并非立即執(zhí)行,而是回調(diào)執(zhí)行的時(shí)候,使用 bind() 方法。而 apply/call 則會(huì)立即執(zhí)行函數(shù)。

再總結(jié)一下:

  • apply 、 call 、bind 三者都是用來(lái)改變函數(shù)的this對(duì)象的指向的;
  • apply 、 call 、bind 三者第一個(gè)參數(shù)都是this要指向的對(duì)象,也就是想指定的上下文;
  • apply 、 call 、bind 三者都可以利用后續(xù)參數(shù)傳參;
  • bind 是返回對(duì)應(yīng)函數(shù),便于稍后調(diào)用;apply 、call 則是立即調(diào)用 。

bind詳細(xì)參考地址:《MDN:Function.prototype.bind()》

原文

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容