What's the third parameter used in jQuery.proxy?

In the jQuery API doc, using the jQuery.proxy function:

jQuery.proxy( function, context )

      


function . The function whose context will be changed.
context The object to which the context (this) of the function is to be given.
jQuery.proxy( context, name )

      


context The object to which the function context is to be set.
name The name of the function whose context will be changed (must be a property of the context object).
proxy : function(fn, proxy, thisObject){
    if ( arguments.length === 2 ) {
        if (typeof proxy === "string" ) {
        thisObject = fn;
        fn = thisObject[proxy];
        proxy = undefined;
    } else if ( proxy && !jQuery.isFunction( proxy ) ) {
        thisObject = proxy;
        proxy = undefined;
    }
}
   if ( !proxy && fn ) {
   proxy = function() {
   return fn.apply( thisObject || this, arguments );
   };
}
// So proxy can be declared as an argument
return proxy;
}

      

But when I look into the jQuery source code, the proxy function. I found that there are 3 parameters declared.

So what I'm wondering is that using the third parameter, can't understand the code :(

I am writing a segment of code to test a function.

var someObj = { somefnc : function() {} };
function fnc() {
    this.somefnc();
    console.log(arguments);
}
var proxyedFnc = jQuery.proxy(fnc, undefined, someObj, "arg1","arg2");
proxyedFnc();
//output: []

      

And I'm wondering why no arguments were passed to fnc ..

+3


source to share


2 answers


Below source comes from jquery-1.7.2.js . Are you sure you are checking the same version between the original and api doc?



// Bind a function to a context, optionally partially applying any
// arguments.
proxy: function( fn, context ) {
    if ( typeof context === "string" ) {
        var 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
    var args = slice.call( arguments, 2 ),
        proxy = function() {
            return fn.apply( context, 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 || proxy.guid || jQuery.guid++;

    return proxy;
},

      

0


source


xdazz is right, the latest version 1.7.2 has a different syntax that also allows a few extra arguments to be entered in apply

and passed to the proxy function, f.ex:

var fn = function() {
    console.log(this.foo, arguments);
};

var obj = {
    foo: 'bar'
};

$.proxy(fn, obj, 'one', 'two')();

      

Running this code will print bar ["one", "two"]



You will get the same result by running:

$.proxy(fn, obj)('one', 'two');

      

I can also add that none of these are documented in the official API, so things can work differently under the hood in different versions. This code has been tested in 1.7.2.

+6


source







All Articles