【发布时间】:2013-02-18 14:54:59
【问题描述】:
我使用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实例,而是触发回调动作的元素。
在调用this.draw 时我无法传递额外参数,因为它是一个回调函数,而onComplete 只有一个参数。
如何从draw 调用show 方法?
【问题讨论】:
标签: javascript oop class callback