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.
Grammar
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
. name
the function should be context
a property of the object.
Parameters
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. |
Precautions:
additionalArguments
the 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