Jquery.proxy () function explanation

Source: Internet
Author: User

jQuery.proxy()function is used to change the context of a function .

You can pass the specified function into the function, which returns a new function whose execution code is unchanged, but the context (this) inside the function has been changed to the specified value.

The function belongs to the global jQuery object.


JQuery 1.4 Adds this static function. jQuery.proxy()There are two ways to use the function:

Usage One :

function, context [, additionalarguments])

Changes the function context object of the function to the specified context .

Usage two :

Jquery.proxy (context, name [, Additionalarguments])

Changes the context of the function named to the name specified context . namethe function should be context a property of the object.


Find the corresponding parameter based on the parameter name defined in the previous syntax section .

Parameters Description
function function types need to change the functions of the context object.
Context Any type specifies the context object that is set for the function.
Name The string type needs to change the name of the function of the context object (it should be a property in the context).
Additionalarguments Optional/any type specifies the arguments that need to be passed when the function is called, and can have any number of arguments.


    • additionalArgumentsthe parameters are supported starting with JQuery 1.6.
    • This method is ideal when attaching an event-handler function to another object with the context of the event handler. In addition, jquery ensures that even if you use the jQuery.proxy() returned "proxy" function to bind the event, jquery can still unbind correctly if you pass in the original function at unbind time.
    • Starting with jquery 1.9, if context null or undefined, the context of the proxy function does not change. This allows only the parameters of the function to be passed in without jQuery.proxy() changing the function's context.
return value

jQuery.proxy()The return value of the function is the XmlDocument type, returning the parsed XML Document object.

Example & Description

The following is the jQuery.proxy() jquery sample code associated with the function to demonstrate jQuery.proxy() the specific use of the function:

//appends a newline label and the specified HTML content to the current pagefunctionW (HTML) {Document.body.innerHTML+ = "<br/>" +html;}varName = "Hello";functionFoo (A, b) {W ( This. Name); W (A+b);} //Call directlyFoo (1, 2 );//Hello//3varobj = {name: "Codeplayer", age:18 };varProxy = $.proxy (foo, obj, 5, 10 );//The agent calls the Foo () function, at which point its internal this points to the object objproxy ();//Codeplayer// the

Jquery.proxy () function explanation

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.