从事件处理程序调用方法

Ayo*_* M. 2 javascript jquery javascript-events

我有这个代码我正在努力,但每次我调用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);
    }
}
Run Code Online (Sandbox Code Playgroud)

nic*_*ine 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

你应该阅读这本书,JavaScript:好的部分,并访问Crockenator的网站crockford.com

您还可以在这里阅读有关JavaScript"this"的问题,http://www.quirksmode.org/js/this.html