code="java"{
if (!Function.prototype.bind) {
Function.prototype.bind = function(obj){
var owner = this, args = Array.prototype.slice.call(arguments),
callobj = Array.prototype.shift.call(args);
return function(e){
e = e || top.window.event || window.event;
owner.apply(callobj, args.concat([e]));
};
};
}
})();[/code]
以上代码在不支持ECMASCRIPT5的浏览器上,实现了ECMASCRIPT5的bind方法
看function12:
http://rainsilence.iteye.com/admin/blogs/894054
[code="javascript"]// 匿名函数执行
(function(){
// 检测是否实现了函数
if (!Function.prototype.bind) {
Function.prototype.bind = function(obj){
// 得到bind执行的参数
var owner = this, args = Array.prototype.slice.call(arguments),
// args虽说可以用数组引用,但是却不是数组,只能用Array的原型去调用数组方法
callobj = Array.prototype.shift.call(args);
// 返回包装apply的闭包。主要功能是this指针绑定到owner上
return function(e){
e = e || top.window.event || window.event;
owner.apply(callobj, args.concat([e]));
};
};
}
})();
[/code]