admin管理员组文章数量:1417070
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
2 Answers
Reset to default 2Hi 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
本文标签: Modifying the event object in JavascriptStack Overflow
版权声明:本文标题:Modifying the event object in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745259344a2650283.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论