sorry for the comment - actually, I had misinterepreted the issue as you described it if the issue was inside the second method.
As the others implied, the event listener is going to call the function with window
as the this
object. Don't worry, even as a JS guru I agree it doesn't make a lot of sense.
While you can simply put everything in closures and put this
into a constant var
(ie, me
or self
) my preference is to customize the call to the function to always have a particular this
reference. You can do this like so:
imageLoader.addEventListener('change', this.handleImage.bind(this), false);
bind() is a relatively new JavaScript function that gives you a new method, representing the original, but called with a particular this
context. Some JavaScript libraries have equivalents that are compatible with older browsers, like Dojo's "hitch". But, since your code involves a <canvas>
anyway, you should be mostly okay for compatibility.