我正在阅读关于jQuery.proxy()
的API文档。它看起来很有前途,但我想知道在什么情况下最好使用它。有人可以启发一下我吗?
我正在阅读关于jQuery.proxy()
的API文档。它看起来很有前途,但我想知道在什么情况下最好使用它。有人可以启发一下我吗?
this
值绑定到特定对象的函数时,比如回调函数,如事件处理程序、AJAX回调、超时、间隔、自定义对象等。Person
的对象,它有一个属性名字。它还链接到一个文本输入元素,每当输入值改变时,此人物对象中的名称也会更新。function Person(el) {
this.name = '';
$(el).change(function(event) {
// Want to update this.name of the Person object,
// but can't because this here refers to the element
// that triggered the change event.
});
}
我们经常使用的一种解决方案是将this上下文存储在变量中,并在回调函数中使用该变量,例如:
function Person(el) {
this.name = '';
var self = this; // store reference to this
$(el).change(function(event) {
self.name = this.value; // captures self in a closure
});
}
另外,我们也可以在这里使用 jQuery.proxy
,这样对 this
的引用将指向 Person 对象,而不是触发事件的元素。
function Person(el) {
this.name = '';
$(el).change(jQuery.proxy(function(event) {
this.name = event.target.value;
}, this));
}
bind
方法,并已在一些浏览器上提供。function Person(el) {
this.name = '';
$(el).change(function(event) {
this.name = event.target.value;
}.bind(this)); // we're binding the function to the object of person
}
self = this
的方法只能在内联创建函数时使用。 - Esailijafunction Person(el) {
this.name = '';
$(el).change((event) => {
this.name = event.target.value;
});
}
- TypingTurtle这只是一种为闭包设置上下文的速记方法,例如:
$(".myClass").click(function() {
setTimeout(function() {
alert(this); //window
}, 1000);
});
然而,我们经常希望this
与我们所在的方法保持不变,这正是$.proxy()
要用的地方,像这样:
$("button").click(function() {
setTimeout($.proxy(function() {
alert(this); //button
}, this), 1000);
});
它通常用于延迟调用,或者在您不想使用冗长的闭包声明方法的任何地方。将上下文指向对象的字符串方法...嗯,我还没有在日常代码中发现实际用途,但我相信有应用场景,只取决于您的对象/事件结构。
例如,如果您想创建回调函数。而不是:
var that = this;
$('button').click(function() {
that.someMethod();
});
你可以这样做:
$('button').click($.proxy(this.someMethod, this));
或者,如果您创建了一个接受回调函数的插件,并且必须为回调函数设置特定上下文。