'Event constants for Javascript?
In Javascript I have code like:
document.addEventListener("mousedown", mouseDownHandler);
and occasionally I might fat finger something like:
document.addEventListener("mouzedown", mouseDownHandler);
And Javascript won't recognize that I'm a moron and I'll be confused why my handler isn't working.
Actionscript3 specifies event constants like so:
MouseEvent.MOUSE_DOWN // equiv to the String "mouseDown"
If I fat-finger a variable or constant then JS gets mad at me and I can solve the problem real quick. Does JavaScript have anything similar to to this or do I need to make my own JS pseudo-constants for event types?
Solution 1:[1]
There are no predefined constants/properties which can be used as to define an event for addEventListener.
Stripped original answer below (and in the revision history):
The Event object defined the following constants. Using Firefox 8.0, and the following code:
alert(Object.keys(Event).map(function(name){return name + "\t" + Event[name]}).join('\n'))
Solution 2:[2]
Maybe use jQuery? Big switch for a small problem, but jQuery has aliased functions for a lot of JS handlers, and if you spell a function name wrong you'll definitely get an error.
That is,
document.addEventListener("mousedown", mouseDownHandler); // right
$(document).mousedown(mouseDownHandler); // right
document.addEventListener("mouzedown", mouseDownHandler); // wrong, no error
$(document).mouzedown(mouseDownHandler); // wrong, throws error
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | nnnnnn |
