Could someone please explain to me how closures work in ActionScript? I am attempting to create a set of Flex Form fields, and assign an event-handler to each of them, however, it seems that after creation, all of the fields have the same event handler.
For example: var i:int = 0; for each (var item:Object in this._myItems) { var f:FormItem = new FormItem(); f.label = item.header; var input:TextInput = new TextInput(); // Setup event-handler var self:MyClass = this; input.addEventListener("change", function (e:Event):void { Alert.show("Event-hanlder #: " + String(i)); }); i++; // Add text input to FormItem f.addChild(input); // Save a reference to the form control item.ui = f; addChild(f); } So I would expect that the number displayed when I change any given field matched the order of the field on the screen. When I run the code, however, no matter which field I adjust, the number is always the same (it is the number of the last form item to be added). It appears that what is happening is that a single function is being created, and each input field has a reference to that same function. Any suggestions? Any help would be greatly appreciated. Thanks! Keith