在某些情况下,我们调用Javascript函数时候,this指针并不一定是我们所期望的那个。例如:
1 //正常的this使用 2 $('#myElement').click(function() { 3 4 // 这个this是我们所期望的,当前元素的this. 5 6 $(this).addClass('aNewClass'); 7 8 }); 9 10 11 //并非所期望的this 12 $('#myElement').click(function() { 13 14 setTimeout(function() { 15 16 // 这个this指向的是settimeout函数内部,而非之前的html元素 17 18 $(this).addClass('aNewClass'); 19 20 }, 1000); 21 22 });
这时候怎么办呢,通常的一种做法是这样的:
1 $('#myElement').click(function() { 2 var that = this; //设置一个变量,指向这个需要的this 3 4 setTimeout(function() { 5 6 // 这个this指向的是settimeout函数内部,而非之前的html元素 7 8 $(that).addClass('aNewClass'); 9 10 }, 1000); 11 12 });
但是,在使用了jquery框架的情况下, 有一种更好的方式,就是使用$.proxy函数。
jQuery.proxy(),接受一个函数,然后返回一个新函数,并且这个新函数始终保持了特定的上下文(context )语境。
有两种语法:
jQuery.proxy( function, context ) /**function将要改变上下文语境的函数。 ** context函数的上下文语境(`this`)会被设置成这个 object 对象。 **/ jQuery.proxy( context, name ) /**context函数的上下文语境会被设置成这个 object 对象。 **name将要改变上下文语境的函数名(这个函数必须是前一个参数 ‘context’ **对象的属性) **/
上面的例子使用这种方式就可以修改成:
$('#myElement').click(function() { setTimeout($.proxy(function() { $(this).addClass('aNewClass'); }, this), 1000); });
可以看一下jQuery中的实现(1.6之前的版本):
/* jQuery 源码之 proxy:
使用 apply 形式, 执行回调函数.
*/
jQuery.proxy = function( fn, proxy, thisObject ) {
if ( arguments.length === 2 ) {
// jQuery.proxy(context, name);
if ( typeof proxy === "string" ) {
thisObject = fn;
fn = thisObject[ proxy ];
proxy = undefined;
/* 转化结果:
thisObject -> context
fn -> name
proxy -> undefined
*/
}
// jQuery.proxy(name, context);
else if ( proxy && !jQuery.isFunction( proxy ) ) {
thisObject = proxy;
proxy = undefined;
}
}
if ( !proxy && fn ) {
/* 使用 proxy 保证 函数执行时, context 为指定值 */
proxy = function() {
return fn.apply( thisObject || this, arguments );
};
}
// Set the guid of unique handler to the same of original handler, so it can be removed
if ( fn ) {
proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
}
// So proxy can be declared as an argument
return proxy;
}
其实就是平常使用的的call和apply,大部分的时候作为回调使用。
在stackoverflow上有个问题,其中的例子比较典型,供参考:
比如有如下代码:
$('#myElement').click(function() {
// In this function, "this" is our DOM element.
$(this).addClass('aNewClass');
});
这里的this就是我们的DOM元素。如果我们要在增加class样式之前需要等待一段时间,可能会将代码写成下面这样(注意:有问题的代码)
$('#myElement').click(function() {
setTimeout(function() {
// Problem! In this function "this" is not our element!
$(this).addClass('aNewClass');
}, 1000);
});
这里的this就不是我们期望的那个DOM元素了。解决方法就是使用jQuery的$.proxy()了,代码如下:
$('#myElement').click(function() {
// ------------------v--------give $.proxy our function,
setTimeout($.proxy(function() {
$(this).addClass('aNewClass'); // Now "this" is again our element
}, this), 1000);
// ---^--------------and tell it that we want our DOM element to be the
// value of "this" in the function
});
我们可以这样来理解上面的代码:
function() {
// v--------func is the function we gave to $.proxy
func.apply( ctx );
// ----------^------ ctx is the value we wanted for "this" (our DOM element)
}
这就体现出来jQuery中$.proxy()的强大功效了。
注意:在jQuery 1.6及之后的版本中,除了上面提到的两种用法之外,proxy还增加了其他两种用法:
jQuery.proxy( function, context [, additionalArguments ] )
jQuery.proxy( context, name [, additionalArguments ] )
具体使用可以参考jQuery手册。