Using built-in closures / delegate functions in ActionScript

Why are inline closures so rarely used in ActionScript? They are very powerful and I think are quite readable. I almost never see anyone using them, so maybe I'm just looking at the wrong code. Google uses them in its sample Google Maps APIs for Flash, but I think this is the only place I've seen them.

I prefer them because you have access to local variables in the area that defines them, and you keep the logic in one method and do not get many functions for which you have to come up with a name.

Are there any tricks to using them? They work almost the same way as in C #.

Actually, I just discovered that AS3 supports them, and I'm very annoyed because I thought I read that they are deprecated in AS #. Therefore, I will return to their use!

private function showPanel(index:int):void {    

_timer = new Timer(1000, 1);        
_timer.addEventListener(TimerEvent.TIMER, function(event:Event):void 
{
    //  show the next panel
    showPanel(index++);
});
+5
source share
3 answers

The biggest issue to keep in mind is that often 'this' is not defined in a string close. Sometimes you can install 'this', but not always the correct 'this' that you could set, depending on how you use them.

But I would say that most of the Flex code that I worked on was constantly expanding throughout the cycle, since callbacks are the only way to get the job done, and often you don't need to identify a whole separate function.

, , ; , , (, ).

, .

+3

, , ( , Flash 9). ( ) - - , .

+2

, , :

http://livedocs.adobe.com/flex/3/html/16_Event_handling_6.html#119539

(This is what Mitch said, since the keyword 'this' goes beyond)

So, this is Adobe's answer, but I have to refer to local variables much more often than to 'this'.

How do others interpret Adobe recommendations?

+1
source

All Articles