Search code examples
javascriptevent-bubbling

Use same function as "standard" one and as an event handler


I have a function

function foo(event, var1) {
    // do something...
}

I am using this function in some event bubbling, but I also need to use as a normal function that's called by me from my code. Event parameter is used only with event bubbling. What to pass to event from my own code to get this to work?

P.s. I have a feeling this is not good design, but that's the way things turned out. We'll change it when time comes, but for now, it's gotta stay like this.

Thank you.


Solution

  • If var1 is always passed to the function then you should switch the order of params.

    If your function goes foo(var1, event) then it doesn't matter if event is empty, the function will still work. This has to do with the concept of optional params.