JavaScript - 'this'在重写的画布onmousedown中

时间:2014-10-13 19:14:44

标签: javascript canvas prototype

我有以下代码段:

VIEW.CTX = function(canvas){
    this.canvas = canvas;
    this.canvas.onmousedown = this.onDocumentMouseDown;

    //more stuff...
};

VIEW.CTX.prototype = {
    constructor: VIEW.CTX
};

以后

VIEW.CTX.prototype.onDocumentMouseDown = function(event){
    console.log(this);
    this.doSomething();
}

当我创建CTX实例并直接调用onDocumentMouseDown时:

var ctx = new VIEW.CTX(theCanvas);
ctx.onDocumentMouseDown('');

'this'指的是我的CTX实例。但是,当我通过单击画布调用onDocumentMouseDown时,'this'指向canvas元素,并且在尝试调用this.doSomething();时出现错误。如何让它引用它的CTX实例而不是canvas元素?

0 个答案:

没有答案