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

使用する ...

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

...代わりに、オブジェクトのコンテキストをコールバック関数に具体的にバインドします。それ以外の場合、この関数はグローバルコンテキストで呼び出され、そのthis関数はを参照しwindowます。

于 2012-09-17T13:26:51.140 に答える