我有这个有错误的小提琴-> http://jsfiddle.net/Osoascam/AkZZr/6/ (这是没有错误的版本)--> http://jsfiddle.net/Osoascam/AkZZr/7 /
其中有一个模块(如主应用程序),一个Module.AjaxInterface
处理 Ajax 调用,一个Module.Modules.Inbox
(执行与电子邮件收件箱相关的任务),以及一个 Module.Pages.Gmail
处理多个模块以显示页面。所有这些都是使用模块模式完成的。
现在,您可以看到有很多回调。我想知道this
这些电话会发生什么...
我没有得到this
参考发生了什么以及如何保存它:
getMessages: function(params) {
var parameters = params || {};
params = {
// Please note I'm using this, which equals the module
successCallback: this.pretendRender,
successCallbackParameters: parameters,
json: params.json
};
var test = new Module.AjaxInterface(params);
test.ajaxCall();
},
因此,对模块本身内部函数的调用是有效的……然后,它调用test.ajaxCalls
,而后者又调用pretendRender()
. 现在,在pretendRende
r 我有这个:
pretendRender: function(data, parameters) {
// LINE 106 that is causing the ERROR
// It says "this.addColor() is not defined and THIS = window now
data.color = this.addColor();
parameters.successCallback(data);
},
addColor: function() {
return "#AD9";
}
我的问题是......this
参考发生了什么?为什么会变成window
? 我该如何解决?我知道我可以使用call
or apply
,但是pretendRender
正在调用该函数AjaxInterface
,并且对 的引用Modules.Inbox
已经丢失(除非我使用caller
,我不能在 下"strict"
)。我知道我可以通过this
保留AjaxInterface
它,但我真正想要的是真正了解正在发生的事情并创建一个优雅的解决方案。