Array Extras for Actionscript
Late last fall, with the advent of Firefox 1.5, Mozilla added a handful of new Array methods with the update of the Gecko browser engine to JavaScript 1.6. Apparently, these have also found their way into ActionScript. Sharp eyed Daniel Hai noted last month that these were silently added into Flex 2.0 Beta 2 as undocumented methods, and this past week, they’ve finally made their debut in the Beta 3’s Array documentation.
These new additions to the Array class bring a couple of item locator methods, indexOf() and lastIndexOf(), which behave much like their corresponding String analogues, and a set of more interesting iterative methods: every(), filter( ) forEach(), map(), and some(). Each of these latter methods take a callback function to be invoked on each item in the array, and collectively, allow arrays to be used much like lists in functional programming.
While these new methods don’t add any new functionality that couldn’t be done in the past via relatively simple loops, they do make working with arrays quite a bit easier and help simplify the idioms used in array operations. In the last few days, I’ve taken to them so much that I even threw together a quick ArrayExtras backport implementing these as mixin methods for ActionScript 2.0.
With the significant retooling and polish that’s been going into ActionScript 3.0, it’s great to see that Adobe is leading the way with E4x support and the incorporation of Mozilla’s Array extras as we move towards the 4th edition of ECMAScript.