AS3中的过载[]运算符 [英] Overload [] operator in AS3

查看:172
本文介绍了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 and for each..in loops on the object to retrieve the desired values."

这篇关于AS3中的过载[]运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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