In the past, I’ve always written very explicit formal event handlers and wired them up “properly”. Lately I’ve been following a couple of shortcuts for the more simple event handlers and I’ve been enjoying the saved typing. The first shortcut is using an anonymous function instead of a proper method.
myButton.addEventListener(MouseEvent.CLICK, function(event:Event) {mainStack.selectedIndex=1;} );
The second method is declaring the event handler right inside my mxml.
<mx:Button width=”256″ height=”256″ horizontalCenter=”-154″ top=”78″ >
<mx:click>
<![CDATA[
mainStack.selectedIndex=1;
]]>
</mx:click>
</mx:Button>

Well, using an anonymous function can be a bit of a double edge sword, especially when you need to execute a handler from two sources. Just me 2 cents.
As mentioned, I use either method in only the simple cases. Anything more involved, such as being invoked from multiple places, deserves a proper handler.
You’re not worried about memory leaks. Maybe in a small app, bu tin anything larger….just seems to me not worth the shortcut.
Huh, never thought about encasing a simple event like the 2nd method. Cool and thanks!
@Alan I’m not sure where this memory leak would occur. The component would have a single reference to the anonymous function. When the component went away there would be no references to that function anymore so it should be a candidate for garbage collection.
The reason I think that… If you do something like this:
myButton.addEventListener(MouseEvent.CLICK, function(event:Event) {mainStack.selectedIndex=1;} ,false,0,true);
(Don’t ever do that!)
It’ll make a weak reference to the anonymous method. That method will immediately become a candidate for GC and you’ll get some very odd bugs with it being collected at seemingly random times. That would make me think that when the component goes away, the only reference to the function would go away.
But maybe I’m missing something important, I admit I don’t know how the internals of the anonymous functions work.