# Flexible Event Handler function

**URL:** <https://forum.kirupa.com/t/flexible-event-handler-function/263976>\
**Category:** flash\
**Created:** [June 21, 2008, 8:22am UTC](https://forum.kirupa.com/t/flexible-event-handler-function/263976 "2008-06-21T08:22:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pawikan](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@pawikan](https://forum.kirupa.com/u/pawikan)\
**Post date:** [June 21, 2008, 8:22am UTC](https://forum.kirupa.com/t/flexible-event-handler-function/263976/1 "2008-06-21T08:22:55Z")

</div>

Is there a way to create a function that handle events (especially MouseEvents) and still call this function as a plain one without passing any arguments? All this time, I just create an extra function that handles the mouseEvent and call the **needed function** inside it.

Example:

function handler(e:MouseEvent):void{  
realFunction();  
}

function realFunction():void{  
// I want this function to work as a handler and at the same time a plain function  
}

Just thought that maybe there’s a better way of doing it.

Thanks in advance. 🙂

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [June 21, 2008, 8:29am UTC](https://forum.kirupa.com/t/flexible-event-handler-function/263976/2 "2008-06-21T08:29:09Z")

</div>

The way that I do this (which is also the way that I’ve seen suggested by others) is to give the event parameter a default value, making it optional:

```auto
function realFunction(e:MouseEvent = null):void{
    // I want this function to work as a handler and at the same time a plain function
}

```

---

<div class="post-metadata">

**Author:** ![pawikan](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@pawikan](https://forum.kirupa.com/u/pawikan)\
**Post date:** [June 21, 2008, 8:32am UTC](https://forum.kirupa.com/t/flexible-event-handler-function/263976/3 "2008-06-21T08:32:16Z")

</div>

Coool. Just tried it and it works fine. Thanks for the quick response. 🙂

---

<div class="post-metadata">

**Author:** ![pawikan](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@pawikan](https://forum.kirupa.com/u/pawikan)\
**Post date:** [June 21, 2008, 8:58am UTC](https://forum.kirupa.com/t/flexible-event-handler-function/263976/4 "2008-06-21T08:58:54Z")

</div>

Oh… wait, a follow-up to a related topic. What about event handlers that can also take an extra custom parameter? Is that possible? If it is then how do you add the event listener?

In cases like this I create a global variable that is used as reference to the event handler. Can you extend an event and add the extra parameter to it. Just thought again that maybe there’s a better way.

Thanks in advance. 🙂

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [June 21, 2008, 10:16am UTC](https://forum.kirupa.com/t/flexible-event-handler-function/263976/5 "2008-06-21T10:16:28Z")

</div>

> What about event handlers that can also take an extra custom parameter? Is that possible?

Sure, you could have such a function:

```auto
public function handler(e:Event = null, extra:Object = null):void {}

```

If you declared the function like that, then you would have to call _handler(null, someExtraValue)_ when calling the function without going through EventDispatcher. When using it as a handler, then you would need to call _addEventListener_ like so:

```auto
addEventListener(SomeEvent.SOME_CONSTANT, function(e:Event):void { handle(e, someExtraValue) });

```

That process could be kind of annoying, so you could also declare the handler as:

```auto
public function handler(...args):void {}

```

… and handle all of the arguments by detecting their type inside of the function.

Instead of adding an extra parameter to the function, I think that most people use custom Event subclasses that have a property that holds the extra value. That way, the value can be accessed with _event.myExtraValue_. Of course, that method doesn’t really lend itself to functions that are supposed to be able to be used without an accompanying event, but you could always add an optional second parameter and check to see whether or not a valid Event reference was passed in and then decide whether or not you want to check the second argument.

If you are wondering whether or not you can convince EventDispatcher to pass additional arguments along with the Event into your handler, then I _think_ that the answer is ‘no’, since _dispatchEvent_ calls the handler at some point, and there’s no way to modify the call that it makes and and override would need to fit the method signature anyway.

---

<div class="post-metadata">

**Author:** ![pawikan](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@pawikan](https://forum.kirupa.com/u/pawikan)\
**Post date:** [June 21, 2008, 2:02pm UTC](https://forum.kirupa.com/t/flexible-event-handler-function/263976/6 "2008-06-21T14:02:25Z")

</div>

I see… thanks for the knowledge share! 🙂
