jQuery.proxy( function, context )Returns: Functionversion deprecated: 3.3
Description: Takes a function and returns a new one that will always have a particular context.
-
version added: 1.4jQuery.proxy( function, context )
-
functionType: Function()The function whose context will be changed.
-
contextType: PlainObjectThe object to which the context (
this
) of the function should be set.
-
-
version added: 1.4jQuery.proxy( context, name )
-
contextType: PlainObjectThe object to which the context of the function should be set.
-
nameType: StringThe name of the function whose context will be changed (should be a property of the
context
object).
-
-
version added: 1.6jQuery.proxy( function, context [, additionalArguments ] )
-
functionType: Function()The function whose context will be changed.
-
contextType: PlainObjectThe object to which the context (
this
) of the function should be set. -
additionalArgumentsType: AnythingAny number of arguments to be passed to the function referenced in the
function
argument.
-
-
version added: 1.6jQuery.proxy( context, name [, additionalArguments ] )
-
contextType: PlainObjectThe object to which the context of the function should be set.
-
nameType: StringThe name of the function whose context will be changed (should be a property of the
context
object). -
additionalArgumentsType: AnythingAny number of arguments to be passed to the function named in the
name
argument.
-
Note: This API has been deprecated in jQuery 3.3; please use the native Function.prototype.bind
method instead.
This method is most useful for attaching event handlers to an element where the context is pointing back to a different object. Additionally, jQuery makes sure that even if you bind the function returned from jQuery.proxy()
it will still unbind the correct function if passed the original.
Be aware, however, that jQuery's event binding subsystem assigns a unique id to each event handling function in order to track it when it is used to specify the function to be unbound. The function represented by jQuery.proxy()
is seen as a single function by the event subsystem, even when it is used to bind different contexts. To avoid unbinding the wrong handler, use a unique event namespace for binding and unbinding (e.g., "click.myproxy1"
) rather than specifying the proxied function during unbinding.
As of jQuery 1.6, any number of additional arguments may be supplied to $.proxy()
, and they will be passed to the function whose context will be changed.
As of jQuery 1.9, when the context
is null
or undefined
the proxied function will be called with the same this
object as the proxy was called with. This allows $.proxy()
to be used to partially apply the arguments of a function without changing the context.
Examples:
Change the context of functions bound to a click handler using the "function, context" signature. Unbind the first handler after first click.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
|
Demo:
Enforce the context of the function using the "context, function name" signature. Unbind the handler after first click.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
|
Demo:
Change the context of a function bound to the click handler,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
|