The Well-Behaved ActionScript 3 Enum

ActionScript 3 doesn't have an enum language construct built in, but that doesn't mean that you can't create your own. That is an enumerated value that is an object with behavior. It's not rocket science, but I've included my attempt to mimic an earlier post, "The Well-Behaved Java 5 Enum." There are some things you should note while viewing the source:

  • There is no such thing as a "private" constructor in AS3. Several work-arounds have been introduced and discussed on other websites. This source doesn't include any of these, but also doesn't prevent someone from including it in their own.
  • Comparing the MenuItem.as to MenuItem.java (in the previously mentioned post), you'll notice that the AS3 version doesn't include the DOUBLE_CHEESE_MEAL value. The AS3 version doesn't allow "inline" overriding of definitions as does the Java enum. I would think it's possible to do in AS3 (passing a function to the constructor orsomethinglikethat), just not the same as in Java 5.
  • I've also added a public static const values:Array which is an array of the defined values - This provides similar benefits as the Java 5 Enum.values() method, but unlike Java, it requires developer maintenance.

It's far from perfect, but please feel free to correct/comment as you like.

Spread the word... These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Slashdot
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Technorati

1 Comment so far

  1. matt on July 31st, 2007

    Thanks - this helped me out tons.

Leave a reply