为什么在 AS3 中,XML 和 XMList 是不相关的(继承方面的)类?
我不禁认为,使 XML 和 XMLList 两者不相关(就像两者直接扩展 Object 一样)是 AS3 核心库中的一个设计缺陷。 当然,让 XML 扩展 XMLList 会是一个更加简洁的系统,其中 XML 被视为只有一个成员的 XMLList?
这还可以避免 E4X 查询可能返回 XML 或 XMLList 的非常烦人的做法,这可能会导致转换错误。
那么,我有什么理由不认为 XML 和 XMLList 被设计为仅将 Object 作为通用类型呢?
I can't help think that making XML ad XMLList both unrelated, as in both extend Object directly, is a design flaw in the AS3 core library. Surely having XML extend XMLList would be a much cleaner system, where XML is considered an XMLList with only one member?
This would also avoid the very annoying practice of an E4X query possibly returning either an XML or XMLList, which can result in a casting error.
So is there any reason I'm not thinking of that XML and XMLList were designed to only have Object as a common type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
似乎根据 E4X 的 ECMA 规范 (p22 )和 XMLList 文档,XMLList 类确实包含 XML 类的方法,但仅限于只有一个成员时。 因此,在执行 E4X 查询时,结果应始终存储在 XMLList 类型的变量中。
事先,由于这些对象的命名,我推测 XML 对象表示 XML 元素数据的复杂树,但根据 spec(p12),这是不正确的,“XML 类型的每个值都代表一个 XML元素、属性、注释、处理指令或文本节点”。
XMLList 类应该用作这两个类型中更通用的类型:“XMLList 类型的值表示 XML 文档、XML 片段或任意集合XML 对象(例如,查询结果)”
因此这解决了我的转换问题,因为我不应该将 E4X 查询存储为 XML 类型的变量。 我应该只使用 XML 类来迭代 XMLLists 并分解 XML 元素。
It seems that according to the ECMA spec for E4X (p22) and the XMLList documentation, that the XMLList class does contain the methods of the XML class, but only when there is one member. So when performing an E4X query the result should always be stored in a variable of type XMLList.
Beforehand, due to the naming of these objects, I presumed that an XML object represented a complex tree of XML element data, but according to the spec(p12), this is not true, "Each value of type XML represents an XML element, attribute, comment, processing-instruction or text node".
It is the XMLList class that should be used as the more generic type of the two: "A value of type XMLList represents an XML document, XML fragment or an arbitrary collection of XML objects (e.g., a query result)"
So this solves my casting issue, as I should have never stored E4X queries as a variable of type XML. I should only use the XML class for iterating over XMLLists and breaking apart XML elements.
因此,我对这个问题的简洁回答是,从继承树看来,是的,这两个类(XMLList、XML)是不相关的。
但是,从 ECMA script/AS3 文档来看,它们是相关的,很可能使用 ECMA script/AS3 的原型系统。
当 XMLList 只有一个成员时,它可以被视为 XML 节点。 我假设 XML 类的方法和字段由 AVM2 插入到 XMLList 原型中。
So my concise answer to this question is that, it seems, from looking at the inheritance tree that, yes the two classes (XMLList, XML) are unrelated.
But, from looking at the ECMA script/AS3 documentation, that they are related, most likely using the prototype system of ECMA script/AS3.
When an XMLList has only one member it can be treated like an XML node. I presume that the methods and fields of the XML class are inserted into the XMLList prototype by the AVM2.
正如您所注意到的,自动更改的 XML 和 XMLList 类型可能会变得非常混乱。 当聪明的程序员(这里指的是 Adobe..)对事物进行方便的包装时,就会发生这种情况。
正在发生的是某种“自动类型转换”。 而且它并不止于 XML 类型。 考虑这个 XML 示例:
假设我将上述 XML 放在名为
myXml
的变量中。 在以下示例中,E4X 返回包含一个项目的 XMLList,我们使用[0]
访问第一个 XML 项目:在此示例中,我们省略
[0]
部分,依靠Flash的自动类型转换。 返回的一项将转换为 XML:但如果该匹配的 XML 节点包含一个简单的文本节点,Flash 会自动将该类型进一步转换为字符串:
并且如果该文本节点可以解释为数字(带有id 2),Flash 将其甚至进一步转换为数字!
哇!
那么,对此该怎么办?
正如您所注意到的,您必须非常小心类型和“方便的”XML 处理。 一个建议的做法是,当您知道需要单个结果时,始终使用数组访问运算符来指向 E4X 结果的第一个 XML 项:
一开始看起来很奇怪,并且增加了代码的冗长性,但好处是您明确表示您正在寻找 one
admin
元素、onesettings
元素和 onesettings
元素>一个email
类型为“work”
的元素。 你会习惯它的外观。另一个技巧是始终记住转换为您期望使用的类型。 一些例子:
As you have noticed, the automatically changing XML and XMLList types can get quite confusing. Such things happen when clever programmers (pointing to Adobe here..) make convenience wrappers around things.
What's going on is an "automatic type conversion" of sorts. And it doesn't stop to the XML type. Consider this example XML:
Let's say I have the above XML in a variable called
myXml
. In the following example the E4X returns an XMLList with one item, and we access the first XML item using[0]
:In this example we leave out the
[0]
part, relying on the automatic type conversion of Flash. The one returned item is converted to XML:But in cases where this one matched XML node contains a simple text node, Flash automatically converts the type even further into a String:
And if the text node can be interpreted as a number (XML node with id 2), Flash converts it even further into a Number!
Woot!
So, what to do about this?
As you have noticed, you have to be very careful with types and the "convenient" XML handling. One suggested practice is to always use the array access operator to point to the first XML item of an E4X result, when you know you want a single result:
It looks weird at first, and adds verbosity to the code, but the benefit is that you're explicitly saying that you're looking for one
admin
element, onesettings
element, and oneemail
element of type"work"
. And you get used to the look of it.Another tip is to always remember to cast to the type you're expecting to use. Some examples:
在 ActionScript 3.0 中,XMLList 是处理 xml 的新方法,其中 XML 类是 AS 2 的东西,因此包含它是为了提供向后兼容性。
如果您编写新代码,您现在应该使用 XMLList。
希望这对
乔恩有帮助
In ActionScript 3.0 XMLList is the new way to handle xml where as the XML class is an AS 2 thing so it was include to provide backwards compatibility.
If your writing new code you should be using the XMLList now.
Hope this helps
Jon