最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the 3rd param in jQuery.proxy used for? - Stack Overflow

programmeradmin1浏览0评论

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;
},
发布评论

评论列表(0)

  1. 暂无评论