admin管理员组

文章数量:1415137

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

jQuery.proxy( function, context )


function The function whose context will be changed.
context The object to which the context (this) of the function should be set.

jQuery.proxy( context, name )


context The object to which the context of the function should be set.
name The name of the function whose context will be changed (should 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 jQuery source code, of function proxy. I found there're 3 parameters declared.

So I wonder what's the use of third param, cannot understand the code :(

I write a code segment to test the 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 wonder why the arguments were not passed to fnc..

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

jQuery.proxy( function, context )


function The function whose context will be changed.
context The object to which the context (this) of the function should be set.

jQuery.proxy( context, name )


context The object to which the context of the function should be set.
name The name of the function whose context will be changed (should 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 jQuery source code, of function proxy. I found there're 3 parameters declared.

So I wonder what's the use of third param, cannot understand the code :(

I write a code segment to test the 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 wonder why the arguments were not passed to fnc..

Share Improve this question asked Mar 22, 2012 at 7:10 ThemeZThemeZ 4931 gold badge5 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

xdazz is right, the latest 1.7.2 version has a different syntax, that also allows multiple extra arguments to be concated into the apply and passed into 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 would get the same result by doing:

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

I might also add that none of this is documented in the official API, so things might work differently "under the hood" in different versions. This code is tested in 1.7.2.

Below is the source es from jquery-1.7.2.js, Are you sure you check the same version between source 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;
},

本文标签: javascriptWhat is the 3rd param in jQueryproxy used forStack Overflow