spring
spring

Reputation: 18487

Are there any issues with using nested functions with eventlisteners?

I just discovered nested functions in AS3 (yeah, late to the party) and am using them in a Flex project. I've always disliked having to use separate functions for essentially modal operations with eventListeners– adds clutter to code and separates operation logic, as well as not being able to easily reference local variables.

The example below for a user selecting a directory seems to work very well and is nice an compact but I am wondering if there are any issues I am not aware of with this approach. Also, with a non-modal operation (e.g. asynchronous like using a Loader), is it possible to use nested functions?

        private var rootDirectory:File;

        protected function rootBtn_clickHandler(event:MouseEvent):void
        {
            var tmp:File = File.desktopDirectory;
            tmp.browseForDirectory("Set Project Folder");
            tmp.addEventListener(Event.SELECT, onUserSelect);
            tmp.addEventListener(Event.CANCEL, onUserCancel);

            function onUserSelect(event:Event):void
            {
                tmp.removeEventListener(Event.SELECT, onUserSelect);
                tmp.removeEventListener(Event.CANCEL, onUserCancel);
                rootDirectory = event.target as File;
            }

            function onUserCancel(event:Event):void
            {
                tmp.removeEventListener(Event.SELECT, onUserSelect);
                tmp.removeEventListener(Event.CANCEL, onUserCancel);
                trace("user canceled");
            }
        }

Upvotes: 3

Views: 127

Answers (1)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

There can be some caveats when using anonymous or nested functions.

The first and most important is garbage collection:

In your example, the only thing keeping your tmp object from being garbage collected is the SELECT and CANCEL listeners themselves. Since you are not setting the weak flag to true, this shouldn't be a problem, however, if you we're using the weak flag (tmp.addEventListener(Event.SELECT, onUserSelect,false,0,true)) then there is a decent change the tmp object would get garbage collected before the user SELECTS or CANCELS a file.

Also, it's imperative that you remove every listener that you attached in this way. You are doing that in your onUserCancel method, so it should be fine, but if you were not, then you would have a memory leak on your hands as every time your click handler ran, another instance of tmp would be created but it would never get garbage collected because of the listeners attached to it.

So to summarize, most people stay away from anonymous/nested methods in AS3 (and I generally/usually recommend that to people) because it's easy to create memory leaks or have your closures garbage collected by accident. There also may or not be performance differences, but I have never ran tests in that regard.

Upvotes: 1

Related Questions