Here it is in a nutshell. I have a function (keystrokeListener) defined for a class (KeystrokeHandler). The idea is I want to addEventListener the keydown event for a particular object using the keystrokeListener method in my object. No matter what I was doing (whether that be declaring the method as a static method, registering the event within the class constructor, or declaring the method name on an instance of the KeystrokeHandler) yielded the following error for a call within keystrokeListener:
Let’s look at my code for just a moment:
What was happening was the event was firing and actually calling the method appropriately. However, it was not resolving properly, as within the event handler, it was unable to find the other methods associated with the object.
The solution to fix this is actually commented out at the bottom of the code sample above. What you have to do is wrap the handler registration in an anonymous function and call it explicitly if you want this to work correctly. More specifically: