javascript之bind使用介紹
請你說說對javascript中apply,call,bind的理解?
首先apply和call是老生常談的東西,但是對于bind,我愣了下,因為這個詞是jquery中使用頻率很高的一個方法,用來給DOM元素綁定事件用的。
為了搞清這個陌生又熟悉的bind,google一下,發(fā)現(xiàn)javascript1.8.5版本中原生實(shí)現(xiàn)了此方法,目前IE9+,ff4+,chrome7+支持此方法,opera和safari不支持(MDN上的說明)。
bind的作用和apply,call類似都是改變函數(shù)的execute context,也就是runtime時this關(guān)鍵字的指向。但是使用方法略有不同。一個函數(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
如果你的瀏覽器暫時不支持此方法,但你又覺得這個很cool,想用,MDN上也給出參考實(shí)現(xiàn), 這個實(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;
};
}
最后幾行代碼,通過prototype chain的方式,其中fBound是調(diào)用bind函數(shù)的子類,為什么這么實(shí)現(xiàn),可以仔細(xì)看 fBound = function(){ return ... }這一部分,其中this是運(yùn)行時決定的,這里主要考慮到如果用new的方式來調(diào)用函數(shù)(構(gòu)造函數(shù)方式)的情況。
下面的例子,就能很好的說明這點(diǎn),為了方便說明,此例子直接來自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)的評論遇到一個問題,用戶點(diǎn)擊“最后一頁”鏈接之后就自動調(diào)取最后一頁的資料來顯示。2009-11-11
基于JavaScript實(shí)現(xiàn)熔巖燈效果導(dǎo)航菜單
這篇文章主要介紹了基于JavaScript實(shí)現(xiàn)熔巖燈效果導(dǎo)航菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
Javascript中eval函數(shù)的詳細(xì)用法與說明
Javascript中eval函數(shù)的詳細(xì)用法與說明...2007-03-03
詳解bootstrap-fileinput文件上傳控件的親身實(shí)踐
這篇文章主要介紹了詳解bootstrap-fileinput文件上傳控件的親身實(shí)踐,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧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ù)返回多個值的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)函數(shù)返回多個值的方法,涉及javascript字典類型的使用技巧,需要的朋友可以參考下2015-06-06
《JavaScript高級程序設(shè)計》閱讀筆記(三) ECMAScript中的引用類型
ECMAScript中的引用類型,主要包括Object類、Boolean類、Number類、String類、instanceof運(yùn)算符2012-02-02

