我使用Class.js來創建類。如何從回調函數中調用正確的類上下文
從一個回調函數invocked時,我沒有得到一個方法內正確的上下文
我的代碼是
WordCloud = MyClass.extend({
init: function(data) {
var me = this;
(......).on("onComplete", this.draw);
},
show: function(word) {
alert(word)
},
draw : function(words){
console.debug(this); // prints element that triggred `onComplete` action
console.debug(words); // "Hi"
console.debug(me); // me is not defined
me.show(words) // Need to call this method
}
});
問題是,當完成一個動作draw
方法解僱,但裏面draw
方法this
不是實際的class
實例,而是觸發回調操作的元素。
我不能通過exta參數,而調用this.draw
,因爲它是一個回調函數而onComplete
只有一個參數。
如何從draw
調用show
方法?