我知道问题出在哪里,但不知道解决此问题的最佳选择是什么。我有一个回调,我无法从它访问this
。我不想在范围之外有任何变量来引用它。我可以通过this
作为参数吗?将此传递给回调
var myModule = Module.create({
init: function() {
ws.subscribe('/topic/notifications', this._wsNotifications, headers);
},
refresh: function() {
},
_wsNotifications: function (message) {
this.refresh(); //Error: 'this' is undefined because it's a callback
}
});
当然你可以,你试过吗? – tymeJV
为什么你不想要一个超出回调范围的变量来保存对此的引用?这是一个完全可以接受的做法。 – DoctorMick
使用'myModule.refresh(); ' – davidkonrad