Flex dictionary literal

前端 未结 1 1729
梦如初夏
梦如初夏 2021-01-27 16:48

Working in Flex I need to populate Dictionaries with a fairly involved structure. Based on this documentation page I tried creating a dictionary literal via the following synta

相关标签:
1条回答
  • 2021-01-27 17:04

    You're not doing anything wrong that's just not the way to make the structure you want. Using something = {} is shorthand for something = new Object(). There is no such shorthand to create a Dictionary.

    From the docs you linked:

    An associative array is an instance of the Object class, which means that each key corresponds to a property name.

    and

    ActionScript 3.0 introduces an advanced type of associative array called a dictionary. Dictionaries, which are instances of the Dictionary class in the flash.utils package, use keys that can be of any data type but are usually instances of the Object class. In other words, dictionary keys are not limited to values of type String.

    To get your expected result you'd have to do the following:

    var defaultMarkingA:Dictionary = new Dictionary();
    defaultMarkingA["type"] = "page";
    defaultMarkingA["displayText"] = "XYZ";
    defaultMarkingA["innerMarkings"] = new Array();
    
    var dict:Dictionary = new Dictionary();
    dict["id"] = "ABC";
    dict["type"] = "page";
    dict["displayText"] = "ABC";
    
    defaultMarkingA["innerMarkings"].push(dict);
    

    There is nothing wrong with using an Object as an associative array. The only problem I could see is that performance may differ between using Object and Dictionary but you'd need to do some profiling to see if a) there is any difference and b) if that difference matters.

    0 讨论(0)
提交回复
热议问题