我正在使用此代码,但每次调用init方法时都会出现错误
this.addElement不是函数
是因为我无法从事件处理程序中调用方法吗?
function editor () {
this.init = function () {
$("#area").bind('click' , this.click_event );
}
this.addElement = function () {
console.log("adding element");
}
this.click_event = function(event) {
this.addElement();
console.log("click event in x : "+event.data);
}
}
答案 0 :(得分:7)
function editor () {
var that = this;
this.init = function () {
$("#area").bind('click' , this.click_event );
}
this.addElement = function () {
console.log("adding element");
}
this.click_event = function(event) {
that.addElement();
console.log("click event in x : "+event.data);
}
}
您应该阅读本书JavaScript: the Good Parts并访问Crockenator的网站crockford.com
您还可以在此处阅读JavaScript“this”问题,http://www.quirksmode.org/js/this.html