AS3中的过载[]运算符 [英] Overload [] operator in AS3
问题描述
我刚刚完成了编写我自己的collecion类,我真的想让它可以迭代与每个或简单的构造,或只是使用集合[key]符号访问元素。
我写了一个 getElementAt(index):MyOwnElement
函数,但使用它,不像使用方括号,甚至不让我开始迭代。
有什么办法吗?
您应该查看 mx.utils.Proxy
- 从中集合类的子类化(并将其设置为动态)可能会让您访问一些您想要的功能(或至少
例如,下面是 nextValue()
方法文档的摘录:
对象的属性枚举为
检索属性值。但是,
不能枚举
Proxy类本身的属性。 此函数
支持在中为...执行
,并为
each..in
循环对象检索所需的值。
I just finished writing my own collecion class, and i'd really like to make it iterable with the for each or the simple for construct, or just to access elements with the collection[key] notation.
I've written a getElementAt(index):MyOwnElement
function, but using it, is not as sexy as using the square brachets, don't even let me start on iterating..
Is there any way?
You should take a look at mx.utils.Proxy
-- subclassing your collection class from that (and setting it as dynamic) might give you access to some of the functionality you want (or at least something that's close enough.)
For example, here's an excerpt from the documentation of the nextValue()
method:
"Allows enumeration of the proxied object's properties by index number to retrieve property values. However, you cannot enumerate the properties of the Proxy class themselves. This function supports implementing
for...in
andfor each..in
loops on the object to retrieve the desired values."
这篇关于AS3中的过载[]运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!