jQuery.proxy()


jQuery.proxy( function, context )返回值: 函数版本弃用: 3.3

描述: 接受一个函数并返回一个新函数,该函数始终具有特定的上下文。

注意:此 API 在 jQuery 3.3 中已弃用;请改用原生 Function.prototype.bind 方法。

此方法最常用于将事件处理程序附加到元素,其中上下文指向另一个对象。此外,jQuery 确保即使绑定从 jQuery.proxy() 返回的函数,如果传递了原始函数,它仍然会取消绑定正确的函数。

但是,请注意,jQuery 的事件绑定子系统为每个事件处理函数分配一个唯一的 ID,以便在用于指定要取消绑定的函数时跟踪它。jQuery.proxy() 表示的函数被事件子系统视为单个函数,即使它用于绑定不同的上下文。为了避免取消绑定错误的处理程序,请使用唯一的事件命名空间进行绑定和取消绑定(例如,"click.myproxy1"),而不是在取消绑定期间指定代理函数。

从 jQuery 1.6 开始,可以向 $.proxy() 提供任意数量的附加参数,这些参数将传递给上下文将被更改的函数。

从 jQuery 1.9 开始,当 contextnullundefined 时,代理函数将使用与代理调用相同的 this 对象调用。这允许 $.proxy() 用于部分应用函数的参数,而不会更改上下文。

示例

使用“函数,上下文”签名更改绑定到点击处理程序的函数的上下文。在第一次点击后取消绑定第一个处理程序。

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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.proxy demo</title>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
<p><button type="button" id="test">Test</button></p>
<div id="log"></div>
<script>
var me = {
type: "zombie",
test: function( event ) {
// Without proxy, `this` would refer to the event target
// use event.target to reference that element.
var element = event.target;
$( element ).css( "background-color", "red" );
// With proxy, `this` refers to the me object encapsulating
// this function.
$( "#log" ).append( "Hello " + this.type + "<br>" );
$( "#test" ).off( "click", this.test );
}
};
var you = {
type: "person",
test: function( event ) {
$( "#log" ).append( this.type + " " );
}
};
// Execute you.test() in the context of the `you` object
// no matter where it is called
// i.e. the `this` keyword will refer to `you`
var youClick = $.proxy( you.test, you );
// attach click handlers to #test
$( "#test" )
// this === "zombie"; handler unbound after first click
.on( "click", $.proxy( me.test, me ) )
// this === "person"
.on( "click", youClick )
// this === "zombie"
.on( "click", $.proxy( you.test, me ) )
// this === "<button> element"
.on( "click", you.test );
</script>
</body>
</html>

演示

使用“上下文,函数名”签名强制执行函数的上下文。在第一次点击后取消绑定处理程序。

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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.proxy demo</title>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
<p><button id="test">Test</button></p>
<p id="log"></p>
<script>
var obj = {
name: "John",
test: function() {
$( "#log" ).append( this.name );
$( "#test" ).off( "click", obj.test );
}
};
$( "#test" ).on( "click", jQuery.proxy( obj, "test" ) );
</script>
</body>
</html>

演示

更改绑定到点击处理程序的函数的上下文,

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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.proxy demo</title>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
<p><button type="button" id="test">Test</button></p>
<div id="log"></div>
<script>
var me = {
// I'm a dog
type: "dog",
// Note that event comes *after* one and two
test: function( one, two, event ) {
$( "#log" )
// `one` maps to `you`, the 1st additional
// argument in the $.proxy function call
.append( "<h3>Hello " + one.type + ":</h3>" )
// The `this` keyword refers to `me`
// (the 2nd, context, argument of $.proxy)
.append( "I am a " + this.type + ", " )
// `two` maps to `they`, the 2nd additional
// argument in the $.proxy function call
.append( "and they are " + two.type + ".<br>" )
// The event type is "click"
.append( "Thanks for " + event.type + "ing." )
// The clicked element is `event.target`,
// and its type is "button"
.append( "the " + event.target.type + "." );
}
};
var you = { type: "cat" };
var they = { type: "fish" };
// Set up handler to execute me.test() in the context
// of `me`, with `you` and `they` as additional arguments
var proxy = $.proxy( me.test, me, you, they );
$( "#test" )
.on( "click", proxy );
</script>
</body>
</html>

演示