What's the use for anonymous functions in AS3?

MKII picture MKII · Feb 16, 2012 · Viewed 8.5k times · Source

I came across them, but I have yet to see why I should use them. Could someone explain it?

Answer

Sat picture Sat · Feb 16, 2012

It is comfortably to use in closures, when you need not name for this function. I. e. this way:

var myFunc : Function = function() : void {trace("Hello world!");}
doSomething(myFunc);

or this:

button.addEventListener(MouseEvent.CLICK,
    function(e : MouseEvent) : void
    {
        // doSomething
    });

You do not have to use them, but you may if it is easier.