Get a static property from class in actionscript

2019-08-04 01:53发布

问题:

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:

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]
}