Proxy: Change this point
Use Method 1:
Function Show () {
alert (this);
}
$.proxy (Show,document) (); Document
Use Method 2:
Function Show (N1,N2) {
Alert (N1);
Alert (n2);
alert (this);
}
$.proxy (show,document) (3,4); Document 3 4
$.proxy (show,document,3,4) (); Document 3 4
$.proxy (show,document,3) (4); Document 3 4
Use Method 3:
var obj = {
Show:function () {
alert (this);
}
};
$ (document). Click ($.proxy (obj, ' Show ')); Object
From the source of the proxy method can be seen, the following code fragment is for ' use Method 3 ' to do the processing, it allows the second argument to a string, in fact, the equivalent of: $ (document). Click ($.proxy (obj.show,obj));
if typeof Context = = = "string" ) { = fn[context ]; = fn; = tmp;}
The following code fragment is for the processing of the parameters, first using Core_slice.call to intercept the 3rd parameter to the last, then get the parameters of the call, and finally merge.
// simulated bindargs = core_slice.call (arguments, 2function() { return fn.apply (Context, Args.concat (Core_slice.call (arguments));};
Proxy Complete code:
varCore_slice =Array.prototype.slice, ..... jquery.extend ( ...) ....... .......--------------//A Global GUID counter for objectsGuid:1, //Bind a function to a context, optionally partially applying any //arguments.Proxyfunction(FN, context) {vartmp, args, proxy; if(typeofContext = = = "string") {tmp=fn[context]; Context=fn; FN=tmp; } //Quick Check to determine if target are callable, in the spec //This throws a TypeError, but we'll just return undefined. if( !jquery.isfunction (FN)) { returnundefined; } //simulated bindargs = Core_slice.call (arguments, 2 ); Proxy=function() { returnfn.apply (Context, Args.concat (Core_slice.call (arguments))); }; //Set the GUID of unique handler to the same of original handler, so it can be removedProxy.guid = Fn.guid = Fn.guid | | jquery.guid++; returnproxy; }, ...............});
jquery Tool Method Proxy