Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying the event object in Javascript

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;
    }
});
like image 673
Hakan Bilgin Avatar asked Jan 21 '12 11:01

Hakan Bilgin


People also ask

What is a change event in JavaScript?

Summary: in this tutorial, you’ll learn about the JavaScript change event of the input text, radio button, checkbox, and select elements. The change event occurs when the element has completed changing.

How to create a custom event in JavaScript?

We create a custom event using the CustomEvent constructor. This takes two arguments, the first is the name of the event and the second is an object that contains the data. The property name inside the object name should be named detail otherwise it won’t work. We create a listener for this event. We trigger or dispatch the event.

How do I execute JavaScript code when an event occurs?

A JavaScript can be executed when an event occurs, like when a user clicks on an HTML element. To execute code when a user clicks on an element, add JavaScript code to an HTML event attribute:

How do I handle the change event of an element?

The change event occurs when the element has completed changing. To attach an event handler to the change event of an element, you can either call the addEventListener () method: element.addEventListener ( 'change', function() { // handle change }); Code language: JavaScript (javascript) or use the onchange attribute of the element. For example:


2 Answers

Hi Hakan you are also here :)

Try create your own event object and then send it using the trigger() function.

Example

//Create a new jQuery.Event object without the "new" operator.
var e = jQuery.Event( "click" );
 
// trigger an artificial click event
jQuery( "body" ).trigger( e );

Trigger

$( "#foo" ).on( "click", function() {
  alert( $( this ).text() );
});
$( "#foo" ).trigger( "click" );

All examples are on those links.

like image 102
Otávio Barreto Avatar answered Sep 27 '22 18:09

Otávio Barreto


The drawback of your solution is that you tied the user to your implementation of event.hotKey. If another library adds it's own implementation of event.hotKey which does a completely different thing than yours, the user who tries to use them both will get some hard to debug problems.

Instead of altering the event object itself, you should provide the functionality separately:

function MyEvent(event) {
  var myEvent = clone(event);
  myEvent.hotKey = ...
  return myEvent;
}

addEvent( document, "keyup", function(event) {
    switch (MyEvent(event).hotKey) {
        case 'meta-c':
            // code here
            break;
        case 'meta-a':
            // code here
            break;
    }
});

If you don't like the fact that the handlers have to wrap the event, then you can change the listening function:

function myAddEvent ( target, type, handler, capture, scope) {
  var myHandler = function(event){
    handler.call(scope,MyEvent(event));
  }
  addEvent(target, type, myHandler, capture);
}

*this is just a draft solution, you still need to consider unlistening and other aspects

like image 25
Tibos Avatar answered Sep 27 '22 18:09

Tibos