Posts Tagged function

How to reuse event functions

This is just a quick tip I picked up while working with a teamate.

I often came across the case that I would have to make a function exactly the same as the function that was called from an event listener because the function called from the listener would have an event as argument but the same function that I wanted to call but not from an event didn’t.

I had found multiple ways around that problem; I would make another function that both previous function would call or I would create only the listener function and when I wanted to call it from elswhere I would pass it an empty event that I would create just for it. Both these solution appeared unelegant to me.

That’s when I saw this in my teamate code:

// let say your listener is this
myButton.addEventListener(Event.COMPLETE, _activateElement, false, 0, true);
//your function looks like this
private function _activateElement(event:Event = null):void{
  //do something that doesn't require the event
//so from anywhere you can just call the same function like this

By simply giving a default value of null to the argument, you can now reuse this function both in the context of an event listener and anywhere else.

, ,


Parse error: syntax error, unexpected ';' in /homepages/25/d169645162/htdocs/wp-content/themes/fusion/footer.php on line 13