Four usage scenarios for Jquery.proxy

Source: Internet
Author: User

Zccst

In fact, there are only two ways to use, but each one and subdivide whether to pass the argument.

Give a piece of HTML, and you'll test it later:

< P ><  ID= "Test">Test</button></p  ><ID= "Log"></p >

1,jquery.proxy (function, context);

Use the context as the function run contexts (that is, this)

2,jquery.proxy (function, context [, additionalarguments]);

Passing parameters to function

Use scene: When click, execute function, in a given context, pass two parameters simultaneously, if an event is required, it can be the third parameter of function.

Note: If the function execution environment does not apply the proxy, then the context will be the current Click Object, now specifies the other context, has no relation with the current click Object.

1 varme = {2   /*I ' m a dog*/3Type: "Dog",4  5   /*Note that event comes *after* one and*/6Testfunction(one, both, event) {7$ ("#log")8       /*' One ' is the first additional parameter, corresponding to the object ' you '*/9. Append (" )Ten       /*' Two ' is the second additional parameter, corresponding to the object ' they '*/ One. Append ("And they are" + Two.type + ".<br>" ) A       /*' This ' is the context, corresponding to the normal object ' me '*/ -. Append ("I am a" + This. Type + "," ) -   the        -       /*The event type is click on "click"*/ -. Append ("Thanks for" + event.type + "ing" ) -       /*' Event.target ' is the clicked element, type is button*/ +. Append ("the" + Event.target.type + ".") ); -   } + }; A   at varyou = {type: ' Cat ' }; - varthey = {type: ' Fish ' }; -   -  - /*There are 4 parameters: The first is function, the second is the context, and the third to fourth is the parameter*/ - varProxy =$.proxy (Me.test, ME, you, they); in  -$ ("#test"). On ("click"), proxy); to /* Run Result: + Hello Cat: - And they is fish. the I am a dog, Thanks for clicking the submit. * */

In this case, click is only equivalent to a trigger button, and the function executed after the trigger is not related to the click Point.

3,jquery.proxy (context, name);

Usage Scenario: Context is a plainobject,name is its method. Executed at click, but the context of the test function execution is obj, not $ ("#test")

1 varobj = {2Name: "John",3Testfunction() {4Console.log ( This);5$ ("#log"). Append ( This. Name);6$ ("#test"). Off ("click"), obj.test);7     }8 };9$ ("#test"). On ("Click", Jquery.proxy (obj, "test"));//executed at Click, but not the context of the Click

Result analysis: After binding a fixed-point event, the binding is removed the first time it executes.

After removal, there are no click events on the button, so clicking on the buttons will not have any reaction.

4,jquery.proxy (context, name [, Additionalarguments]);

Related Article

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.