Last Updated: February 25, 2016
·
4.365K
· blake barrett

Remove event listeners from anonymous functions

One issue with using anonymous functions as event handlers is that you cannot remove the listener, which ultimately hinders garbage collection.
A simple way around it is by using the EMCAScript "arguments" object.

event.currentTarget.removeEventListener(event.type, arguments.callee);

So for example:

public function showWindow():void
{
    var popup:NativeWindow = new NativeWindow();
    popup.addEventListener(Event.CLOSE, function(event:Event):void {
        event.currentTarget.removeEventListener(event.type, arguments.callee);
        trace("the window was closed");
    }
}

http://blog.blakebarrett.net/2011/04/generic-event-listener-removal-from-any.html