javascript之bind使用介紹
更新時間:2011年10月09日 00:12:05 作者:
首先apply和call是老生常談的東西,但是對于bind,我愣了下,因為這個詞是jquery中使用頻率很高的一個方法,用來給DOM元素綁定事件用的
前幾天看到一個面試題,題目是這樣的:
請你說說對javascript中apply,call,bind的理解?
首先apply和call是老生常談的東西,但是對于bind,我愣了下,因為這個詞是jquery中使用頻率很高的一個方法,用來給DOM元素綁定事件用的。
為了搞清這個陌生又熟悉的bind,google一下,發(fā)現(xiàn)javascript1.8.5版本中原生實現(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上也給出參考實現(xiàn), 這個實現(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ù)的子類,為什么這么實現(xiàn),可以仔細(xì)看 fBound = function(){ return ... }這一部分,其中this是運行時決定的,這里主要考慮到如果用new的方式來調(diào)用函數(shù)(構(gòu)造函數(shù)方式)的情況。
下面的例子,就能很好的說明這點,為了方便說明,此例子直接來自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
請你說說對javascript中apply,call,bind的理解?
首先apply和call是老生常談的東西,但是對于bind,我愣了下,因為這個詞是jquery中使用頻率很高的一個方法,用來給DOM元素綁定事件用的。
為了搞清這個陌生又熟悉的bind,google一下,發(fā)現(xiàn)javascript1.8.5版本中原生實現(xiàn)了此方法,目前IE9+,ff4+,chrome7+支持此方法,opera和safari不支持(MDN上的說明)。
bind的作用和apply,call類似都是改變函數(shù)的execute context,也就是runtime時this關(guān)鍵字的指向。但是使用方法略有不同。一個函數(shù)進(jìn)行bind后可稍后執(zhí)行。
例子如下:
復(fù)制代碼 代碼如下:
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ù)的例子:
復(fù)制代碼 代碼如下:
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上也給出參考實現(xiàn), 這個實現(xiàn)很有意思,代碼如下:
復(fù)制代碼 代碼如下:
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ù)的子類,為什么這么實現(xiàn),可以仔細(xì)看 fBound = function(){ return ... }這一部分,其中this是運行時決定的,這里主要考慮到如果用new的方式來調(diào)用函數(shù)(構(gòu)造函數(shù)方式)的情況。
下面的例子,就能很好的說明這點,為了方便說明,此例子直接來自MDN:
復(fù)制代碼 代碼如下:
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ù)的作用實例介紹
- 淺談javascript中call()、apply()、bind()的用法
- JS中改變this指向的方法(call和apply、bind)
- 深入理解JS中的Function.prototype.bind()方法
相關(guān)文章
javascript之典型高階函數(shù)應(yīng)用介紹二
在前一篇文章javascript之典型高階函數(shù)中主要實現(xiàn)了幾個典型的functional函數(shù),文章最后也提出了疑問,為啥那樣的實現(xiàn)與F#之類的函數(shù)式語言“不太一樣”呢?今天來試試更“函數(shù)式”的實現(xiàn)2013-01-01快速獲取/設(shè)置iframe內(nèi)對象元素的幾種js實現(xiàn)方法
下面小編就為大家?guī)硪黄焖佾@取/設(shè)置iframe內(nèi)對象元素的幾種js實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05解讀input標(biāo)簽的value屬性及name屬性
這篇文章主要介紹了解讀input標(biāo)簽的value屬性,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01