Get a static property from class in actionscript

2019-08-04 01:40发布

I have this class

package somePackage
{
    public class SomeClass
    {
        public static const FOO: SomeClass = new SomeClass("0");
        public static const BAR: SomeClass = new SomeClass("1");
        }
}

I want to be able to get those static property given it's name.

Example:

public static function getProperty(propertyName: String): SomeClass {
    //don't know what goes here
}

var property1:SomeClass = SomeClass.getProperty("FOO"); // property1 == SomeClass.FOO
var property2:SomeClass = SomeClass.getProperty("BAR"); // property2 == SomeClass.Bar

1条回答
仙女界的扛把子
2楼-- · 2019-08-04 02:27

You could use square brackets like this:

SomeClass['FOO'] 

Or if you want to put it in a method that returns a typed object:

public static function getProperty(propertyName: String):SomeClass {
    return SomeClass[propertyName]
}
查看更多
登录 后发表回答