【发布时间】:2009-06-25 12:05:58
【问题描述】:
我有这样的事情:
var Something = function(){
this.render = function(){};
$(window).resize(function(){
this.render();
});
}
问题在于匿名函数内部的“this”指的是窗口对象。我知道我可以这样做:
var Something = function(){
this.render = function(){};
var tempThis = this;
$(window).resize(function(){
tempThis.render();
});
}
但是有更好的方法吗?这看起来不太优雅。
【问题讨论】:
-
您应该在“tempThis = this”之前放置一个“var”以确保正确的范围。
标签: javascript jquery this