可对函数进行如下扩展
Function.prototype.bind = function(obj) {
var _this = this;
return function() {
_this.apply(obj,arguments);
}
}
用法如下
var a = function(){
alert(this.title)
}.bind(document);
a();
常用在这儿
function myalert() {
this.title = 'hello world';
this.init = function() {
$("#xxx").click(this.close.bind(this));
}
this.close = function() {
alert(this.title)
}
}
<P style="PADDING-BOTTOM: 0px; LINE-HEIGHT: 22px; MARGIN-TOP: 0px; FONT-FAMILY: 微软雅黑, 宋体, 'Trebuchet MS', Tahoma, Arial, sans-serif; MARGIN-BOTTOM: 10px; COLOR: rgb(34,34,34); FONT-SIZE: 14px; PADDING-TOP: 0px" class=p1>var a = new myalert();</P><P style="PADDING-BOTTOM: 0px; LINE-HEIGHT: 22px; MARGIN-TOP: 0px; FONT-FAMILY: 微软雅黑, 宋体, 'Trebuchet MS', Tahoma, Arial, sans-serif; MARGIN-BOTTOM: 10px; COLOR: rgb(34,34,34); FONT-SIZE: 14px; PADDING-TOP: 0px" class=p1>a.init();</P>