In Javascript, the common understanding is that you shouldn't modify objects you don't own. I understand and agree with the arguments. Dispite that, I am considering to add a couple of properties the Event object. First I'll write a little background as well as what the implications will be and and after that I'm hoping to get some intelligent feedback from you, with pro's and con's.
I have written an event-manager, which have methods like addEvent, removeEvent, etc. When events are fired, I'll call handlers, passing on the event object to the event-handlers passed in when eventlisteners has been registered.
Before passing on the event object, I will add a couple of properties to the event-object. For instance; "hotKey" - with possible values such as "shift-c", "alt-d" or "meta-a". (The last one corresponds to "ctrl-a" on Window and "cmd+a" on MacOS, etc. In other words, depending on OS it will match correctly and the handlers logical pattern doesn't need to care about whether it's on which OS or what meta-key it is)
The implications is that the handlers code get smaller thanks to the fact that all the needed "if-then" statements has been executed by the event-manager, when for instance when key-related events has been fired. The event-handler pretty much needs to have an switch-statement and do the fun stuff.
Besides the hotKey-property, I am planning to add some other properties as well, mostly to simplify the event-handler functions and thereby eliminate commonly executed logic by event-handlers. In order to avoid naming colissions, I might add the new properties in a sub-ojbect. In other words, the properties does not need to be top-level properties.
I'm implementing this at the moment but I'm hoping to get insights I might not have been thinking of.
ADDITION
The question is what other people think of the implementation below. This requires that the event-object, that is passed to the handler, needs to modified. I am asking the question since I'm actually modifying an object that I don't own...but in this case there are advantages of doing so.
addEvent( document, "keyup", function(event) {
switch (event.hotKey) {
case 'meta-c':
// code here
break;
case 'meta-a':
// code here
break;
}
});