开发者

Order of items in objects in ActionScript

开发者 https://www.devze.com 2022-12-14 12:58 出处:网络
Try to compile: var object:Object = {}; object.one = \"foo\"; object.two = \"foo\";开发者_开发问答

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?


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.


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.


What "messed up" order?

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

0

精彩评论

暂无评论...
验证码 换一张
取 消