Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overload [] operator in AS3

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?

like image 302
Carlo Avatar asked Jan 23 '23 22:01

Carlo


1 Answers

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."

like image 114
hasseg Avatar answered Jan 26 '23 11:01

hasseg