Flex 字典字面量 [英] Flex dictionary literal

查看:21
本文介绍了Flex 字典字面量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Flex 中工作我需要用相当复杂的结构填充字典.基于 这个文档页面,我尝试创建一个字典文字通过以下语法:

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 syntax:

    var defaultMarkingA =
        {
            type: 'page',
            displayText: 'XYZ',
            innerMarkings: [
                {id: 'ABC', type: 'page', displayText: 'ABC'}
            ]
        };

我期望这段代码创建的是一个包含三个映射的嵌套字典结构:

What I expect this code to create is a nested Dictionary structure with three mappings:

  1. 类型"->页面"
  2. "displayText" -> "XYZ"
  3. "innerMarkings" => 带有另一个字典的单元素数组,其中包含:
  1. "type" -> "page"
  2. "displayText" -> "XYZ"
  3. "innerMarkings" => single-element array with another Dictionary inside containing:
  1. "id" -> "ABC"
  2. 类型"->页面"
  3. "displayText" -> "ABC"

根据getQualifiedClassName,代码实际创建的是一个对象".我在这里做错了什么?actionscript 是否无法处理嵌套的字典文字?

What the code actually creates is an "Object" according to getQualifiedClassName. What am I doing wrong here? Is actionscript unable to deal with a nested Dictionary literal?

推荐答案

您没有做错任何事情,这只是不是制作您想要的结构的方式.使用 something = {}something = new Object() 的简写.没有这样的速记来创建Dictionary.

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.

来自您链接的文档:

关联数组是 Object 类的一个实例,这意味着每个键对应一个属性名称.

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

ActionScript 3.0 引入了一种高级类型的关联数组称为字典.字典,它们是flash.utils 包中的字典类,使用的键可以是任何数据类型,但通常是 Object 类的实例.其他词,字典键不限于 String 类型的值.

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);

使用 Object 作为关联数组并没有错.我能看到的唯一问题是使用 ObjectDictionary 之间的性能可能有所不同,但您需要进行一些分析以查看 a) 是否有任何差异和 b) 如果这种差异很重要.

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.

这篇关于Flex 字典字面量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆