Overload event handler in javascript

user

I've got an event handler in javascript that I'd like to overload :

function overloadedHandler(event, myObject){
    if(event){
        var elem = event.currentTarget;
    } else {
        var elem = myObject.x;
    }
    //function logic
}

$('.mydiv').on('click', overloadedHandler);

If I want to call the overloadedHandler from another function (not as an event handler), what's the right way to do it ?

1. overloadedHandler(null, obj);
2. overloadedHandler(undefined, obj);
3. overloadedHandler(false, obj);

P.S. I understand that I could put //function logic part in another function & call that when needed outside of an event handler, but I would like to know if there's anything wrong with the above approach?

Djizeus

Well indeed it seems a bit odd at first sight, but I would say that there is nothing wrong. It is basically equivalent to having an argument as optional, except that the event handling mechanism forces you to define the event as first argument so you cannot make it optional.

To make it as close as possible to an optional argument, I would set the event as undefined when calling it not as an event handler, since this is what the value would be if it was an optional argument. null would also be OK I guess, but I think false is semantically wrong: you want to say that there is no event, not that the event is false.

One way to make it maybe even cleaner would call it with an event argument made for the purpose. In your example, that would be:

overloadedHandler({currentTarget: obj.x}, obj);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript event handler is not working

From Dev

event handler in javascript

From Dev

javascript remove event in handler,

From Dev

Change event fires jquery handler but not javascript handler

From Dev

Range Slider Event Handler Javascript

From Dev

Using this in event handler in strict javascript?

From Dev

Event Handler Namespace in Vanilla JavaScript

From Dev

Javascript event handler (mouseover) not firing

From Dev

JavaScript button event handler not working

From Dev

Javascript custom event with user handler

From Dev

Disable javascript in browser event handler

From Dev

Javascript Closure Event Handler Issue

From Dev

Access variable from event handler in javascript object

From Java

JavaScript click event handler not fired in the code below

From Dev

Why in Javascript event handler functions with parentheses?

From Dev

Titanium JavaScript event handler and class scope

From Dev

Javascript splice while push is in an event handler

From Dev

Different behaviours with events in javascript with and without an event handler

From Dev

Execute javascript code at the end of event handler

From Dev

calling different functions in the same javascript event handler

From Dev

Javascript Event handler - why did this work?

From Dev

What is the way in order not to kill event handler in javascript?

From Dev

How to make a Javascript event handler to execute first?

From Dev

Debugging JavaScript & css code: adding an event handler

From Dev

JavaScript - Call internal method from event handler

From Dev

How to find a Javascript function if it is event handler?

From Dev

How to make a Javascript event handler to execute first?

From Dev

Bind event handler to "console.log" JavaScript event

From Dev

Passing event to event handler

Related Related

  1. 1

    Javascript event handler is not working

  2. 2

    event handler in javascript

  3. 3

    javascript remove event in handler,

  4. 4

    Change event fires jquery handler but not javascript handler

  5. 5

    Range Slider Event Handler Javascript

  6. 6

    Using this in event handler in strict javascript?

  7. 7

    Event Handler Namespace in Vanilla JavaScript

  8. 8

    Javascript event handler (mouseover) not firing

  9. 9

    JavaScript button event handler not working

  10. 10

    Javascript custom event with user handler

  11. 11

    Disable javascript in browser event handler

  12. 12

    Javascript Closure Event Handler Issue

  13. 13

    Access variable from event handler in javascript object

  14. 14

    JavaScript click event handler not fired in the code below

  15. 15

    Why in Javascript event handler functions with parentheses?

  16. 16

    Titanium JavaScript event handler and class scope

  17. 17

    Javascript splice while push is in an event handler

  18. 18

    Different behaviours with events in javascript with and without an event handler

  19. 19

    Execute javascript code at the end of event handler

  20. 20

    calling different functions in the same javascript event handler

  21. 21

    Javascript Event handler - why did this work?

  22. 22

    What is the way in order not to kill event handler in javascript?

  23. 23

    How to make a Javascript event handler to execute first?

  24. 24

    Debugging JavaScript & css code: adding an event handler

  25. 25

    JavaScript - Call internal method from event handler

  26. 26

    How to find a Javascript function if it is event handler?

  27. 27

    How to make a Javascript event handler to execute first?

  28. 28

    Bind event handler to "console.log" JavaScript event

  29. 29

    Passing event to event handler

HotTag

Archive