Order of items in objects in ActionScript

纵然是瞬间 提交于 2020-01-16 00:35:28

问题


Try to compile:

var object:Object = {};

object.one = "foo";
object.two = "foo";
object.three = "foo";
object.four = "foo";

for(var key:String in object)
{
    trace(key);
}

... you will get:

one
four
two
three

Why the messed up order?


回答1:


The keys of an object are not ordered. If you need to preserve order and have a lookup, then you need to create a custom collection that provides that functionality.




回答2:


Object in AS3 can be seen as a hashtable where the field name is the key. So you cant rely on the creation order to get the same order when loop throught the field, the order will depend on the algorithm used for hashing the field name.




回答3:


What "messed up" order?

There is no order to properties on an object. for...in can iterate over them in whatever order it likes.



来源:https://stackoverflow.com/questions/1949733/order-of-items-in-objects-in-actionscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!