为什么jqueryproxy有这样的功能?

为什么jqueryproxy有这样的功能?,jquery,proxy,Jquery,Proxy,在jquery代理fn中 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 th

在jquery代理fn中

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 = slice.call( arguments, 2 );
    proxy = function() {
        return fn.apply( context || this, args.concat( 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;
},

语法2(context,string)的主要用法是什么?

它确保传入的
context
变量在将
fn
对象内的数据(带有
context
键)分配给
tmp
变量之前以参数形式存在(也是字符串)


不这样做,如果我们只调用
$.proxy(fn)
,而没有
上下文
参数,
fn
变量将以
undefined
结束,代理函数本身将返回
undefined
,而不是简单地忽略
上下文
变量,并将其作为一开始就没有传入的变量进行处理。

请参见-参见jQuery.proxy文档中的第二个示例。