0

Is there any benefit to removing the event parameter from event handlers if it's not used? I have the following instance:

<s:Button rollOver="handleRollOver(event)" rollOut="handleRollOut(event)"/>

And the following code:

private function handleRollOver(event:Event):void
{
    // doing stuff
}

private function handleRollOut(event:Event):void
{
    // doing stuff
}

Is there any benefit to removing the event object if it's not used? For example, changing it to this:

<s:Button rollOver="handleRollOver()" rollOut="handleRollOut()"/>

And the following code:

private function handleRollOver():void
{
    // doing stuff
}

private function handleRollOut():void
{
    // doing stuff
}

The reason I ask is for a couple of reasons. One is that I heard that events bubble through all the display objects so if you can stop them that saves some CPU (not sure how much). But that would involve calling event.stopProp() type of calls correct not merely and at capture time rather than bubble time? Also, I heard that typing the event helps save CPU, so event:Event is slower than event:MouseEvent. I can try and find sources but until then maybe someone knows what I'm talking about. Thanks

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • 2
    I think you'll have to look at the generated code. In ActionScript it is impossible to create an event listener without the event as an argument. I assume MXML turns into an event listener [with event argument] which does nothing but calls your handler w/ no argument. So, my intuition is that there is no benefit to removing the event argument; and in fact there may even be a detriment (AKA Extra methods) – JeffryHouser Oct 17 '12 at 21:27

2 Answers2

1

I don't believe there is any benefit, but there is no downside either. It works, nothing will error out. You just won't have access to event properties. In the case of MXML, make sure you do not pass an event, otherwise you will get an error.

Regardless, it is best practice to always pass events, regardless if you need to or not. It may be more convenient, but it makes your code less flexible and less recognizable when searching through it.

If you are looking for a way to call the event handler without including an event, you can do this:

private function clickHandler(e:MouseEvent = null):void{

}

Setting a default value of null for the event argument allows you bypass that requirement with zero issues. At this point, every single event handler I set up has a default value of null, sometimes with added arguments too, just so that I can call it manually if I need to.

Josh
  • 8,079
  • 3
  • 24
  • 49
1

I consider it good practice to always pass the event parameter, because you might need them at a later point - for example, if you decide to add logging, or when you are debugging, etc. It is valuable information, and it should not be discarded easily.

The performance issues from event bubbling are often greatly exaggerated, anyway: A mouse click event bubbles exactly once for each click, and unless you've stacked a couple of thousand display objects, your system will not become sluggish because of it. Of course, there are other events that happen more often or rapidly, and if you notice glitches, event handling should be one of the first places to check. But unless it is an obvious necessity, worrying about anything other than Event.ENTER_FRAME seems like premature optimization to me.

Having said that - you can safely prevent any event from bubbling by manually stopping propagation within the handler:

private function clickHandler ( ev:Event ) : void {
    ev.stopImmediatePropagation();
}
weltraumpirat
  • 22,544
  • 5
  • 40
  • 54