jquery Tool Method Proxy

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.