0

Flash 或 Flex 中是否存在从其属性链中的对象获取值的现有类?

例如,像这样:

private function labelFunction(item:Object, column:GridColumn):String {
     // where dataField equals "fields.status.name"
     var value:String = PropertyChain.getValue(field, column.dataField);
     return value;
}

~~~~更新~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~
我在 Binding 类中也发现了这个私有方法,它可能可以在自定义类中使用:

/**
 *  @private
 */
private static function getFirstWord(destStr:String):String
{
    // indexPeriod and indexBracket will be equal only if they
    // both are -1.
    var indexPeriod:int = destStr.indexOf(".");
    var indexBracket:int = destStr.indexOf("[");
    if (indexPeriod == indexBracket)
        return destStr;

    // Get the characters leading up to the first period or
    // bracket.
    var minIndex:int = Math.min(indexPeriod, indexBracket);
    if (minIndex == -1)
        minIndex = Math.max(indexPeriod, indexBracket);

    return destStr.substr(0, minIndex);
}
4

1 回答 1

3

我认为没有现有的功能。但它很容易构建,并且不必局限于通用源,因为任何Object对象的任何成员都可以使用方括号表示法按名称检索。这个简单的版本不做任何验证:

public static function getByName(root:*, member:String):* {
    var memlist:Array = member.split('.');
    var temp:* = root;
    for(var i:uint = 0; i < memlist.length; i++)
        temp = temp[memlist[i]];
    return temp;
}

// And you can use this even on strongly-typed values, such as a MovieClip:
trace("stageWidth =", SomeUtil.getByName(mc, "stage.stageWidth"));
于 2012-09-05T20:57:43.327 回答