I have the following code snippet:
VIEW.CTX = function(canvas){
this.canvas = canvas;
this.canvas.onmousedown = this.onDocumentMouseDown;
//more stuff...
};
VIEW.CTX.prototype = {
constructor: VIEW.CTX
};
and later
VIEW.CTX.prototype.onDocumentMouseDown = function(event){
console.log(this);
this.doSomething();
}
When I create an instance of CTX and call onDocumentMouseDown directly:
var ctx = new VIEW.CTX(theCanvas);
ctx.onDocumentMouseDown('');
'this' refers to my instance of CTX. However, when I invoke onDocumentMouseDown by clicking on the canvas, 'this' refers to the canvas element, and I get an error when it tries to call this.doSomething();
. How do I make it refer to its instance of CTX instead of the canvas element?