1

in jquery proxy 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;
},

what is the main usage for the syntax 2, (context, string)?

asked Mar 18, 2014 at 15:41
2

1 Answer 1

1

It's ensuring the passed in context variable is present as an argument (and is also a string) before assigning the data within the fn object with a key of context to the tmp variable.

Without doing this, if we just called $.proxy(fn) with no context argument, the fn variable would end up as undefined and the proxy function itself would return undefined instead of simply ignoring the context variable altogether and processing as if it weren't passed in in the first place.

answered Mar 18, 2014 at 15:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.