【发布时间】:2015-06-29 22:29:25
【问题描述】:
我有一个模型 js 文件,看起来像
goog.provide('model.ErrorLogger');
/**
* @constructor
*/
model.ErrorLogger = function() {
window.onerror = goog.bind(this.errorHandler, this);
this.doInitialSend();
};
goog.addSingletonGetter(model.ErrorLogger);
model.ErrorLogger.prototype.ws_ErrLgr_config = true;
model.ErrorLogger.prototype.doInitialSend = function(){
if (this.ws_ErrLgr_config){
window.setInterval(this.sendReport, this.ws_ErrLgr_config);
}
};
model.ErrorLogger.prototype.sendReport = function(){
// the value of 'this' needs to be of the ErrorLogger model and not windows
if (!this.ws_ErrLgr_config || this.stopped) {
//some more code here
}
}
在构造函数中,我调用了设置 window.setInterval 的 doInitialSend 函数。现在在 sendReport 函数中,“this”的值不正确。如何正确传递 'this' 以获得正确的值,而不是获取窗口的 this。 我尝试将 this 的值存储在引用中,但这也不起作用。例如
var that = this;
window.setInterval(that.sendReport, that.ws_ErrLgr_config);
【问题讨论】:
标签: javascript closures google-closure-compiler google-closure google-closure-library