Typescript Cannot read property 'addEventListener' of undefined
<p>I fail to understang what is missing. The following code will return this error:</p>

<blockquote>
  <p>Uncaught TypeError: Cannot read property 'addEventListener' of
  undefined"</p>
</blockquote>

<pre><code>class Editor{
    public context: CanvasRenderingContext2D;
    constructor( public canvas: HTMLCanvasElement){
        this.context = this.canvas.getContext("2d");
        this.canvas.addEventListener("mousedown", this.handleMouseDown);
    }
    handleMouseDown(ev: MouseEvent){
        this.canvas.addEventListener("mousemove", this.handleMouseMove);
        //this.handleMouseMove(ev);
    }
    handleMouseMove(ev: MouseEvent){
        console.log(ev);
        console.log(ev.target);
    }
}

//==============================================================================
//declare variables.

let mapEditor = new Editor(&lt;HTMLCanvasElement&gt;document.getElementById("map-editor"));
</code></pre>

<p>while this one does not:</p>

<pre><code>let cnv = document.getElementById("map-editor") as HTMLCanvasElement;
cnv.addEventListener("mousedown", handleMouseDown);

function handleMouseDown(ev: MouseEvent){
    ev.target.addEventListener("mousemove", handleMouseMove);
    handleMouseMove(ev);
};

function handleMouseMove(ev: MouseEvent){
    console.log(ev);
    console.log(ev.target);
};
</code></pre>

<p>I've tried to look around and change the order a little bit but I can't get the event to fire. Perhaps I missing an important element of the language?</p>

<p><strong>Edit</strong>:
I think that when the mousedown event is triggered, the context changed and thus the "this" keyword no longer refer to the class instance. Therefor this.canvas is undefined. I will try to change the way I bind methode with event.</p>

<p>I found an explanation here: <a href="https://github.com/Microsoft/TypeScript/wiki/&#39;this&#39;-in-TypeScript/" rel="nofollow">'this'-in-TypeScript</a></p>