这是怎么回事,当我在AS3(对象一)使用? [英] What's happening when I use for(i in object) in AS3?

查看:137
本文介绍了这是怎么回事,当我在AS3(对象一)使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要遍历一个对象在AS3中,你可以使用的属性的(VAR我:字符串对象)是这样的:

To iterate over the properties of an Object in AS3 you can use for(var i:String in object) like this:

对象:

var object:Object = {

    thing: 1,
    stuff: "hats",
    another: new Sprite()

};

循环:

for(var i:String in object)
{
    trace(i + ": " + object[i]);
}

结果:


stuff: hats
thing: 1
another: [object Sprite]

在属性被选中的顺序却似乎有所不同,永远不匹配任何东西,我能想到的,如英文字母属性名称,它们被创建的顺序,等等。事实上,如果我尝试了一些不同的时间在不同的地方,顺序是完全不同的。

The order in which the properties are selected however seems to vary and never matches anything that I can think of such as alphabetical property name, the order in which they were created, etc. In fact if I try it a few different times in different places, the order is completely different.

是否有可能访问给定顺序的属性?这是怎么回事?

Is it possible to access the properties in a given order? What is happening here?

推荐答案

我张贴这作为一个答案只是恭维 BoltClock的答案直接看Flash播放器源$ C ​​$ C ,提供一些额外的见解。事实上,我们可以看到AVM code,专门提供了这个功能,并且它是用C ++编写。我们可以在里面ArrayObject.cpp看到下面的code:

I'm posting this as an answer just to compliment BoltClock's answer with some extra insight by looking directly at the flash player source code. We can actually see the AVM code that specifically provides this functionality and it's written in C++. We can see inside ArrayObject.cpp the following code:

// Iterator support - for in, for each
Atom ArrayObject::nextName(int index)
{
    AvmAssert(index > 0);

    int denseLength = (int)getDenseLength();
    if (index <= denseLength)
    {
        AvmCore *core = this->core();
        return core->intToAtom(index-1);
    }
    else
    {
        return ScriptObject::nextName (index - denseLength);
    }
}

正如你可以看到,当有一个合法财产(对象)返回,它是从 ScriptObject 类抬头,特别是 nextName ()方法。如果我们看一下这些方法中ScriptObject.cpp:

As you can see when there is a legitimate property (object) to return, it is looked up from the ScriptObject class, specifically the nextName() method. If we look at those methods within ScriptObject.cpp:

Atom ScriptObject::nextName(int index)
{
    AvmAssert(traits()->needsHashtable());
    AvmAssert(index > 0);

    InlineHashtable *ht = getTable();
    if (uint32_t(index)-1 >= ht->getCapacity()/2)
        return nullStringAtom;
    const Atom* atoms = ht->getAtoms();
    Atom m = ht->removeDontEnumMask(atoms[(index-1)<<1]);
    if (AvmCore::isNullOrUndefined(m))
        return nullStringAtom;
    return m;
}

我们可以看到,的确如人们所指出的,这里说的虚拟机使用的是哈希表。然而,在这些功能有提供一个具体的指标,这将表明,乍看之下,必有那么具体顺序。

We can see that indeed, as people have pointed out here that the VM is using a hash table. However in these functions there is a specific index supplied, which would suggest, at first glance, that there must then be specific ordering.

如果你深入挖掘(我不会发布的所有code此处)也有从参与中/每个功能,其中之一是方法<$ C不同的类中的方法整体转换$ C> ScriptObject :: nextNameIndex()基本上拉动了整个哈希表,只是开始提供索引表中有效的对象和递增的参数提供的原始索引,只要下一个值指向一个有效的对象。如果我是正确的在我的跨pretation,这将是你的背后随机查找原因,我不相信会有这里任何办法强迫这些操作的标准化/有序图。

If you dig deeper (I won't post all the code here) there are a whole slew of methods from different classes involved in the for in/for each functionality and one of them is the method ScriptObject::nextNameIndex() which basically pulls up the whole hash table and just starts providing indices to valid objects within the table and increments the original index supplied in the argument, so long as the next value points to a valid object. If I'm right in my interpretation, this would be the cause behind your random lookup and I don't believe there would be any way here to force a standardized/ordered map in these operations.

来源
对于那些谁可能想获取源$ C ​​$下的flash播放器的开源部分,你可以从下面的善变库抓住它(你可以下载一个snapshop拉链像github上,这样你就不必安装水银):

Sources
For those of you who might want to get the source code for the open source portion of the flash player, you can grab it from the following mercurial repositories (you can download a snapshop in zip like github so you don't have to install mercurial):

http://hg.mozilla.org/tamarin-central - 这是稳定或释放库

http://hg.mozilla.org/tamarin-central - This is the "stable" or "release" repository

http://hg.mozilla.org/tamarin-redux - 这是发展科。最近更改AVM将在这里找到。这包括Android和这样的支持。 Adobe正在仍在更新和开源的Flash播放器的这些部分,所以这是很好的电流和官方的东西。

http://hg.mozilla.org/tamarin-redux - This is the development branch. The most recent changes to the AVM will be found here. This includes the support for Android and such. Adobe is still updating and open sourcing these parts of the flash player, so it's good current and official stuff.

虽然我吧,这可能是利益,以及: HTTP:/ /$c$c.google.com/p/redtamarin/ 。它是一个支链的关闭(和相当成熟)版本的AVM和可以用来编写服务器端动作。整洁的东西,有一吨的信息,让洞察AVM的运作,所以我想我会包括这一点。

While I'm at it, this might be of interest as well: http://code.google.com/p/redtamarin/. It's a branched off (and rather mature) version of the AVM and can be used to write server-side actionscript. Neat stuff and has a ton of information that gives insight into the workings of the AVM so I thought I'd include it too.

这篇关于这是怎么回事,当我在AS3(对象一)使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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