1

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...

}

4

1 回答 1

5

Use ...

success: Ext.bind(this.readConfigCallback, this)

... instead to bind the object's context to the callback function specifically. Otherwise this function will be called in the global context, and its this function will refer to window.

于 2012-09-17T13:26:51.140 回答