Modifying the event object in Javascript - Stack Overflow

In Javascript, the mon understanding is that you shouldn't modify objects you don't own. I un

In Javascript, the mon 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 monly 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;
    }
});

In Javascript, the mon 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 monly 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;
    }
});
Share Improve this question edited Aug 23, 2014 at 16:51 tshepang 12.5k25 gold badges98 silver badges139 bronze badges asked Jan 21, 2012 at 11:11 Hakan BilginHakan Bilgin 1,12212 silver badges12 bronze badges 1
  • 1 If you're just asking if it's ok to modify the event object, I think this is belongs to codereview – Esailija Commented Jan 21, 2012 at 12:42
Add a ment  | 

2 Answers 2

Reset to default 2

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.

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 pletely 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745259344a4619138.html

相关推荐

  • Modifying the event object in Javascript - Stack Overflow

    In Javascript, the mon understanding is that you shouldn't modify objects you don't own. I un

    3小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信