Good day!
I noticed that in anonymous event handler this is referenced to global, not my class. I know that I can use outer referenced variables (because it creates closures), but how to get right this context?
Simple example:
_movieClipClassVariable = new MyCustomSpriteSubclass();
_movieClipClassVariable.addEventListener(MouseEvent.CLICK, function(event:Event):void {
trace(this); //gives 'global'
});
What about memory usage and garbage collection objects with anonymous handlers? Is declaring handlers as class method better?
I think most of the time declaring a method inside the class is the better choice, (because you don’t have to think about the scope, to remove listeners, to not getting accidentally garbage collected, …) however there are cases where an anonymous function might be a concise and clear approach. It depends.
For instance: