我如何访问闭包“内部函数”中的实例变量对于这种风格:this['varName']?

发布于 2025-01-04 01:27:34 字数 549 浏览 2 评论 0原文

如何访问闭包(内部函数)中的实例变量这种风格: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

违心° 2025-01-11 01:27:34

方括号可用于访问对象(所有内容)的属性,就像您在上面所做的那样:

object[propertyName]

或者设置属性,假设您的类是动态或已经具有您尝试设置的属性:

object[propertyName] = 10;

对于对象,您实际上可以使用 for(String in Object) 循环来输出您想要的所有属性:

var object:Object = {
    test: "string",
    another: 5
};

for(var i:String in object)
{
    trace(i + ": " + object[i]);
}

至于您当前的代码,重组它:

public class Test extends Sprite
{
    private var a0:String = 'a0';
    private var a1:String = 'a1';
    private var a2:String = 'a2';

    public function Test()
    {
        testFun();
    }

    public function testFun():void
    {
        for(var i:uint = 0; i < 3; i += 1)
        {
            trace(this['a'+i]);
        }
    }
}

如果您必须保留您的内部函数,将当前 Test 实例的引用解析到其中:

var testFun:Function = function(subject:Test):void
{
    for(var i:uint = 0; i < 3; i += 1)
    {
        trace(subject['a'+i]);//how to access instance variables?
    }
};

然后:

testFun(this);

Square brackets can be used to access properties of an Object (everything) exactly as you've done above:

object[propertyName]

Or set properties, assuming that your class is either dynamic or already has the property you're trying to set:

object[propertyName] = 10;

With an Object, you're actually able to use a for(String in Object) loop to output all of the properties you want:

var object:Object = {
    test: "string",
    another: 5
};

for(var i:String in object)
{
    trace(i + ": " + object[i]);
}

As for your current code, restructure it:

public class Test extends Sprite
{
    private var a0:String = 'a0';
    private var a1:String = 'a1';
    private var a2:String = 'a2';

    public function Test()
    {
        testFun();
    }

    public function testFun():void
    {
        for(var i:uint = 0; i < 3; i += 1)
        {
            trace(this['a'+i]);
        }
    }
}

If you must retain your inner function, parse a reference to your current instance of Test into it:

var testFun:Function = function(subject:Test):void
{
    for(var i:uint = 0; i < 3; i += 1)
    {
        trace(subject['a'+i]);//how to access instance variables?
    }
};

And then:

testFun(this);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文