我正在使用Class.js来创建类。
从回调函数调用时,我没有在方法中获得正确的上下文
我的代码是
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只有一个参数。
我怎样才能调用该show方法draw?