The this keyword in inline functions



I just spent about half an hour trying to figure out why the inline function I used as a parameter for callLater wasn't being executed properly (not even when I tried setTimeout instead).
After this short timespan of mental agony someone pointed out to me that I had to leave out the this keyword of my inline function. And I thought most scope-related problems would have fled with the arrival of Actionscript3.0...

So for the sake of clarity, this is wrong:

Actionscript:
  1. callLater (function():void{
  2.   this.myVar = "bla-die-bla";
  3. });

..and this is right:

Actionscript:
  1. callLater (function():void{
  2.   myVar = "bla-die-bla";
  3. });

..well that is unless you intend to execute from within the global scope.

No Responses

Note that comments are displayed in reverse chronological order with topmost comments being freshest. Subscribe | Comment

Leave a Reply