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(jquery)利用函數(shù)修改全局變量的代碼
現(xiàn)在博客系統(tǒng)的評(píng)論遇到一個(gè)問(wèn)題,用戶(hù)點(diǎn)擊“最后一頁(yè)”鏈接之后就自動(dòng)調(diào)取最后一頁(yè)的資料來(lái)顯示。2009-11-11
基于JavaScript實(shí)現(xiàn)熔巖燈效果導(dǎo)航菜單
這篇文章主要介紹了基于JavaScript實(shí)現(xiàn)熔巖燈效果導(dǎo)航菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Javascript中eval函數(shù)的詳細(xì)用法與說(shuō)明
Javascript中eval函數(shù)的詳細(xì)用法與說(shuō)明...2007-03-03
詳解bootstrap-fileinput文件上傳控件的親身實(shí)踐
這篇文章主要介紹了詳解bootstrap-fileinput文件上傳控件的親身實(shí)踐,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
JS插件amCharts實(shí)現(xiàn)繪制柱形圖默認(rèn)顯示數(shù)值功能示例
這篇文章主要介紹了JS插件amCharts實(shí)現(xiàn)繪制柱形圖默認(rèn)顯示數(shù)值功能,結(jié)合實(shí)例形式分析了amCharts插件繪制柱形圖并顯示數(shù)值的相關(guān)操作技巧,需要的朋友可以參考下2019-11-11
JavaScript實(shí)現(xiàn)函數(shù)返回多個(gè)值的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)函數(shù)返回多個(gè)值的方法,涉及javascript字典類(lèi)型的使用技巧,需要的朋友可以參考下2015-06-06
JS生成登錄驗(yàn)證碼的實(shí)現(xiàn)示例
本文主要介紹了JS生成登錄驗(yàn)證碼的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
《JavaScript高級(jí)程序設(shè)計(jì)》閱讀筆記(三) ECMAScript中的引用類(lèi)型
ECMAScript中的引用類(lèi)型,主要包括Object類(lèi)、Boolean類(lèi)、Number類(lèi)、String類(lèi)、instanceof運(yùn)算符2012-02-02

