如何在调用我的回调函数时保留this
?
init: function() {
console.log( this ); // constructor, YES!
this.readConfig();
},
readConfig: function() {
console.log( this ); // constructor, YES!
this.httpApi.request({
module : 'monitor',
func : 'getConfig',
success : this.readConfigCallback,
scope : this
});
},
readConfigCallback: function(oResponse) {
console.log( this ); // Window, NO!
var oView = this.getView(); // error...
}
答案 0 :(得分:5)
使用......
success: Ext.bind(this.readConfigCallback, this)
...而是专门将对象的上下文绑定到回调函数。否则,此函数将在全局上下文中调用,其this
函数将引用window
。