[转] 深入浅出 妙用Javascript中apply、call、bind
[From]?http://www.admin10000.com/document/6711.html
?
網(wǎng)上文章雖多,大多復(fù)制粘貼,且晦澀難懂,我希望能夠通過這篇文章,能夠清晰的提升對(duì)apply、call、bind的認(rèn)識(shí),并且列出一些它們的妙用加深記憶。
apply、call?
在 javascript 中,call 和 apply 都是為了改變某個(gè)函數(shù)運(yùn)行時(shí)的上下文(context)而存在的,換句話說,就是為了改變函數(shù)體內(nèi)部 this 的指向。
JavaScript 的一大特點(diǎn)是,函數(shù)存在「定義時(shí)上下文」和「運(yùn)行時(shí)上下文」以及「上下文是可以改變的」這樣的概念。
先來一個(gè)栗子:
| 1 2 3 4 5 6 7 8 9 10 11 | 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 方法,那么我們可以通過 call 或 apply 用 apple 的 say 方法:
| 1 2 3 4 5 | 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 沒有某個(gè)方法(本栗子中banana沒有say方法),但是其他的有(本栗子中apple有say方法),我們可以借助call或apply用其它對(duì)象的方法來操作。
apply、call 的區(qū)別
對(duì)于 apply、call 二者而言,作用完全一樣,只是接受參數(shù)的方式不太一樣。例如,有一個(gè)函數(shù)定義如下:
| 1 2 3 | var func = function(arg1, arg2) { ????? }; |
就可以通過如下方式來調(diào)用:
| 1 2 | func.call(this, arg1, arg2); func.apply(this, [arg1, arg2]) |
其中 this 是你想指定的上下文,他可以是任何一個(gè) JavaScript 對(duì)象(JavaScript 中一切皆對(duì)象),call 需要把參數(shù)按順序傳遞進(jìn)去,而 apply 則是把參數(shù)放在數(shù)組里。
JavaScript 中,某個(gè)函數(shù)的參數(shù)數(shù)量是不固定的,因此要說適用條件的話,當(dāng)你的參數(shù)是明確知道數(shù)量時(shí)用 call 。
而不確定的時(shí)候用 apply,然后把參數(shù) push 進(jìn)數(shù)組傳遞進(jìn)去。當(dāng)參數(shù)數(shù)量不確定時(shí),函數(shù)內(nèi)部也可以通過 arguments 這個(gè)數(shù)組來遍歷所有的參數(shù)。
為了鞏固加深記憶,下面列舉一些常用用法:
1、數(shù)組之間追加
| 1 2 3 4 | 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] */ |
2、獲取數(shù)組中的最大值和最小值
| 1 2 3 | 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 本身沒有 max 方法,但是 Math 有,我們就可以借助 call 或者 apply 使用其方法。
3、驗(yàn)證是否是數(shù)組(前提是toString()方法沒有被重寫過)
| 1 2 3 | functionisArray(obj){ ????returnObject.prototype.toString.call(obj) === '[object Array]' ; } |
4、類(偽)數(shù)組使用數(shù)組方法
| 1 | var domNodes = Array.prototype.slice.call(document.getElementsByTagName("*")); |
Javascript中存在一種名為偽數(shù)組的對(duì)象結(jié)構(gòu)。比較特別的是?arguments 對(duì)象,還有像調(diào)用?getElementsByTagName?,?document.childNodes?之類的,它們返回NodeList對(duì)象都屬于偽數(shù)組。不能應(yīng)用 Array下的 push , pop 等方法。
但是我們能通過 Array.prototype.slice.call 轉(zhuǎn)換為真正的數(shù)組的帶有 length 屬性的對(duì)象,這樣 domNodes 就可以應(yīng)用 Array 下的所有方法了。
深入理解運(yùn)用apply、call
下面就借用一道面試題,來更深入的去理解下 apply 和 call 。
定義一個(gè) log 方法,讓它可以代理 console.log 方法,常見的解決方法是:
| 1 2 3 4 5 | 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是最好的,方法如下:
| 1 2 3 4 5 | function log(){ ??console.log.apply(console, arguments); }; log(1);??? //1 log(1,2);??? //1 2 |
接下來的要求是給每一個(gè) log 消息添加一個(gè)"(app)"的前輟,比如:
| 1 | log("hello world");??? //(app)hello world |
該怎么做比較優(yōu)雅呢?這個(gè)時(shí)候需要想到arguments參數(shù)是個(gè)偽數(shù)組,通過 Array.prototype.slice.call 轉(zhuǎn)化為標(biāo)準(zhǔn)數(shù)組,再使用數(shù)組方法unshift,像這樣:
| 1 2 3 4 5 6 | function log(){ ??var args = Array.prototype.slice.call(arguments); ??args.unshift('(app)'); ?? ??console.log.apply(console, args); }; |
bind
說完了 apply 和 call ,再來說說bind。bind() 方法與 apply 和 call 很相似,也是可以改變函數(shù)體內(nèi) this 的指向。
MDN的解釋是:bind()方法會(huì)創(chuàng)建一個(gè)新函數(shù),稱為綁定函數(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ù)來調(diào)用原函數(shù)。
直接來看看具體如何使用,在常見的單體模式中,通常我們會(huì)使用 _this , that , self 等保存 this ,這樣我們可以在改變了上下文之后繼續(xù)引用到它。?像這樣:
| 1 2 3 4 5 6 7 8 9 10 | var foo = { ????bar : 1, ????eventBind: function(){ ????????var _this = this; ????????$('.someClass').on('click',function(event) { ????????????/* Act on the event */ ????????????console.log(_this.bar);???? //1 ????????}); ????} } |
由于 Javascript 特有的機(jī)制,上下文環(huán)境在 eventBind:function(){ } 過渡到?$('.someClass').on('click',function(event) {?}) 發(fā)生了改變,上述使用變量保存 this?這些方式都是有用的,也沒有什么問題。當(dāng)然使用 bind() 可以更加優(yōu)雅的解決這個(gè)問題:
| 1 2 3 4 5 6 7 8 9 | var foo = { ????bar : 1, ????eventBind: function(){ ????????$('.someClass').on('click',function(event) { ????????????/* Act on the event */ ????????????console.log(this.bar);????? //1 ????????}.bind(this)); ????} } |
在上述代碼里,bind() 創(chuàng)建了一個(gè)函數(shù),當(dāng)這個(gè)click事件綁定在被調(diào)用的時(shí)候,它的 this 關(guān)鍵詞會(huì)被設(shè)置成被傳入的值(這里指調(diào)用bind()時(shí)傳入的參數(shù))。因此,這里我們傳入想要的上下文 this(其實(shí)就是 foo ),到 bind() 函數(shù)中。然后,當(dāng)回調(diào)函數(shù)被執(zhí)行的時(shí)候, this 便指向?foo?對(duì)象。再來一個(gè)簡(jiǎn)單的栗子:
| 1 2 3 4 5 6 7 8 9 | 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í)的全局作用域。
有個(gè)有趣的問題,如果連續(xù) bind() 兩次,亦或者是連續(xù) bind() 三次那么輸出的值是什么呢?像這樣:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | var bar = function(){ ????console.log(this.x); } var foo = { ????x:3 } var sed = { ????x:4 } var func = bar.bind(foo).bind(sed); func(); //? ?? var fiv = { ????x:5 } var func = bar.bind(foo).bind(sed).bind(fiv); func(); //? |
答案是,兩次都仍將輸出 3 ,而非期待中的 4 和 5 。原因是,在Javascript中,多次 bind() 是無(wú)效的。更深層次的原因, bind() 的實(shí)現(xiàn),相當(dāng)于使用函數(shù)在內(nèi)部包了一個(gè) call / apply ,第二次 bind() 相當(dāng)于再包住第一次 bind() ,故第二次以后的 bind 是無(wú)法生效的。
apply、call、bind比較
那么 apply、call、bind 三者相比較,之間又有什么異同呢?何時(shí)使用 apply、call,何時(shí)使用 bind 呢。簡(jiǎn)單的一個(gè)栗子:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 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)。
也就是說,區(qū)別是,當(dāng)你希望改變上下文環(huán)境之后并非立即執(zhí)行,而是回調(diào)執(zhí)行的時(shí)候,使用 bind() 方法。而 apply/call 則會(huì)立即執(zhí)行函數(shù)。
再總結(jié)一下:
- apply 、 call 、bind 三者都是用來改變函數(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)用 。
本文實(shí)例出現(xiàn)的所有代碼,在我的github上可以下載。
轉(zhuǎn)載于:https://www.cnblogs.com/pekkle/p/7083046.html
總結(jié)
以上是生活随笔為你收集整理的[转] 深入浅出 妙用Javascript中apply、call、bind的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux时间子系统之(十二):peri
- 下一篇: 浅入深出之Java集合框架(中)