How can I access instance variables in closure(inner function) for this style: this['varName']?
public class Test extends Sprite
{
private var a0:String = 'a0';
private var a1:String = 'a1';
private var a2:String = 'a2';
public function Test()
{
var testFun:Function = function(evt:Event):void
{
for(var i:uint = 0; i < 3; i += 1)
{
trace(this['a'+i]);//how to access instance variables?
}
};
this.addEventListener(Event.ENTER_FRAME, testFun);
}
}
Square brackets can be used to access properties of an
Object(everything) exactly as you’ve done above:Or set properties, assuming that your class is either
dynamicor already has the property you’re trying to set:With an Object, you’re actually able to use a
for(String in Object)loop to output all of the properties you want:As for your current code, restructure it:
If you must retain your inner function, parse a reference to your current instance of
Testinto it:And then: