• May


    字符串与数字作为键时,优先转换成数字索引优先,数字索引从 0 开始:

    import flash.utils.Dictionary;
    var d:Dictionary = new Dictionary();
    var obj1:Object = new Object();
    var obj2:Object = new Object();
    d["a"] = 1;//a string
    d[0] = "1";//0 number
    d["1"] = 1;//1 number
    d[1] = 1;//1 number
    d[-1] = "2";//-1 string
    d["-1"] = -1;//-1 string
    for(var key:* in d)
     trace(key , typeof key);

    上面看起来好像是加了 6 个键,但实际上因为 Key 会自动转型,所以只有 4 个键。当字符串"1"出现在键时,数字段先的原则,会变成 1;当数字 -1 出现在键时,因为数字索引从 0 开始,所以无法变成数字索引,变成了字符串的键"-1"。



    以下内容转自《programming flex 3》

    ActionScript also has the flash.utils.Dictionary class, which can be used in a similar fashion to Object. However, the keys for Dictionary are evaluated using strict equality for nonprimitive types, and objects used as keys must point to the same identity. In contrast, when using an Object as an associative array, the keys are tested using standard equality for nonprimitives, and objects used as keys can point to different identities as long as the toString() methods return the same value. Consider the following example.