javascript之bind使用介紹
請(qǐng)你說(shuō)說(shuō)對(duì)javascript中apply,call,bind的理解?
首先apply和call是老生常談的東西,但是對(duì)于bind,我愣了下,因?yàn)檫@個(gè)詞是jquery中使用頻率很高的一個(gè)方法,用來(lái)給DOM元素綁定事件用的。
為了搞清這個(gè)陌生又熟悉的bind,google一下,發(fā)現(xiàn)javascript1.8.5版本中原生實(shí)現(xiàn)了此方法,目前IE9+,ff4+,chrome7+支持此方法,opera和safari不支持(MDN上的說(shuō)明)。
bind的作用和apply,call類(lèi)似都是改變函數(shù)的execute context,也就是runtime時(shí)this關(guān)鍵字的指向。但是使用方法略有不同。一個(gè)函數(shù)進(jìn)行bind后可稍后執(zhí)行。
例子如下:
var person = {
name: 'Andrew',
job: 'web front end developer',
gender: 'male',
sayHello: function() {
return 'Hi, I am ' + this.name + ', a ' + this.job;
}
}
console.log(person.sayHello()); // Hi, I am Andrew, a web front end developer
var anotherGuySayHello = person.sayHello.bind({
name:'Alex',
job: 'back end C# developer'
});
console.log(anotherGuySayHello()); // Hi, I am Alex, a back end C# developer
另外帶有參數(shù)的例子:
function add(arg1, arg2, arg3, arg4) {
return arg1 + ' ' + arg2 + ' ' + arg3 + ' ' + arg4;
}
var addMore = add.bind({}, 'a', 'b');
console.log(addMore('c', 'd')); // a b c d
如果你的瀏覽器暫時(shí)不支持此方法,但你又覺(jué)得這個(gè)很cool,想用,MDN上也給出參考實(shí)現(xiàn), 這個(gè)實(shí)現(xiàn)很有意思,代碼如下:
if(!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if(typeof this !== 'function') {
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var fSlice = Array.prototype.slice,
aArgs = fSlice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP ? this : oThis || window, aArgs.concat(fSlice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
最后幾行代碼,通過(guò)prototype chain的方式,其中fBound是調(diào)用bind函數(shù)的子類(lèi),為什么這么實(shí)現(xiàn),可以仔細(xì)看 fBound = function(){ return ... }這一部分,其中this是運(yùn)行時(shí)決定的,這里主要考慮到如果用new的方式來(lái)調(diào)用函數(shù)(構(gòu)造函數(shù)方式)的情況。
下面的例子,就能很好的說(shuō)明這點(diǎn),為了方便說(shuō)明,此例子直接來(lái)自MDN:
function Point(x,y) {
this.x = x;
this.y = y;
}
Point.prototype.toString = function() {
return this.x + ',' + this.y;
};
var p = new Point(1, 2);
p.toString(); // 1,2
var emptyObj = {};
var YAxisPoint = Point.bind(emptyObj, 0);
var axisPoint = new YAxisPoint(5);
axisPoint.toString(); // 0, 5
axisPoint instanceof Point; // true
axisPoint instanceof YAxisPoint; // true
最后給出文章鏈接,方便您進(jìn)一步了解
MDN: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
- javascript中的Function.prototye.bind
- js apply/call/caller/callee/bind使用方法與區(qū)別分析
- js bind 函數(shù) 使用閉包保存執(zhí)行上下文
- JavaScript中的prototype.bind()方法介紹
- js設(shè)置組合快捷鍵/tabindex功能的方法
- javascript bind綁定函數(shù)代碼
- javascript中bind函數(shù)的作用實(shí)例介紹
- 淺談javascript中call()、apply()、bind()的用法
- JS中改變this指向的方法(call和apply、bind)
- 深入理解JS中的Function.prototype.bind()方法
相關(guān)文章
javascript之典型高階函數(shù)應(yīng)用介紹二
在前一篇文章javascript之典型高階函數(shù)中主要實(shí)現(xiàn)了幾個(gè)典型的functional函數(shù),文章最后也提出了疑問(wèn),為啥那樣的實(shí)現(xiàn)與F#之類(lèi)的函數(shù)式語(yǔ)言“不太一樣”呢?今天來(lái)試試更“函數(shù)式”的實(shí)現(xiàn)2013-01-01JS拖拽排序插件Sortable.js用法實(shí)例分析
這篇文章主要介紹了JS拖拽排序插件Sortable.js用法,結(jié)合實(shí)例形式分析了拖拽排序插件Sortable.js功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-02-02JavaScript引用類(lèi)型Date常見(jiàn)用法實(shí)例分析
這篇文章主要介紹了JavaScript引用類(lèi)型Date常見(jiàn)用法,結(jié)合實(shí)例形式分析了引用類(lèi)型Date基本創(chuàng)建、參數(shù)使用及相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-08-08js實(shí)現(xiàn)購(gòu)物車(chē)加減以及價(jià)格計(jì)算功能
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)購(gòu)物車(chē)加減以及價(jià)格計(jì)算功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08快速獲取/設(shè)置iframe內(nèi)對(duì)象元素的幾種js實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇快速獲取/設(shè)置iframe內(nèi)對(duì)象元素的幾種js實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05解讀input標(biāo)簽的value屬性及name屬性
這篇文章主要介紹了解讀input標(biāo)簽的value屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01