我如何访问闭包“内部函数”中的实例变量对于这种风格:this['varName']?
如何访问闭包(内部函数)中的实例变量这种风格: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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
方括号可用于访问
对象
(所有内容)的属性,就像您在上面所做的那样:或者设置属性,假设您的类是
动态
或已经具有您尝试设置的属性:对于对象,您实际上可以使用
for(String in Object)
循环来输出您想要的所有属性:至于您当前的代码,重组它:
如果您必须保留您的内部函数,将当前
Test
实例的引用解析到其中:然后:
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
dynamic
or 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
Test
into it:And then: