我读了this,我明白了,但是,有没有办法附加一个函数来始终在特定范围内执行函数别名,而函数别名的用户不必使用笨拙的.apply stuff?
答案 0 :(得分:2)
您可以使用新的ECMAScript 5 .bind()
方法。不支持它的浏览器的替代实现(取自我链接到的MDC文档):
// Function.prototype.bind polyfill
if ( !Function.prototype.bind ) {
Function.prototype.bind = function( obj ) {
if(typeof this !== 'function') // closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
var slice = [].slice,
args = slice.call(arguments, 1),
self = this,
nop = function () {},
bound = function () {
return self.apply( this instanceof nop ? this : ( obj || {} ),
args.concat( slice.call(arguments) ) );
};
bound.prototype = this.prototype;
return bound;
};
}