看一下 jQuery 源码:
proxy: function( fn, context ) {
var tmp, args, proxy;
if ( typeof context === "string" ) {
tmp = fn[ context ];
context = fn;
fn = tmp;
}
// Quick check to determine if target is callable, in the spec
// this throws a TypeError, but we will just return undefined.
if ( !jQuery.isFunction( fn ) ) {
return undefined;
}
// Simulated bind
args = core_slice.call( arguments, 2 );
proxy = function() {
return fn.apply( context || this, args.concat( core_slice.call( arguments ) ) );
};
// Set the guid of unique handler to the same of original handler, so it can be removed
proxy.guid = fn.guid = fn.guid || jQuery.guid++;
return proxy;
},
如果你删除缓存代码并让它更短一点,你基本上得到.apply()
(我想我正确地翻译了切片代码):
proxy: function(fn, context) {
var args = [].slice.call(arguments, 2);
return function() {
return fn.apply(context || this, args.concat([].slice.call(arguments)));
};
}