Gotcha with ActionScript ‘const’

ActionScript (like JavaScript) lets you access object properties using two different notations. Consider the following class

class A
     public const CONSTANT:int = 34;
var a:A = new A();


The CONSTANT property of object a can be referred using the following two formats:

  • a["CONSTANT"]

Since CONSTANT is declared using const, it cannot be assigned a new value later on. So the following code doesn’t compile (as expected):

a.CONSTANT = 23;


However, the following code compiles and only throws an exception at run-time:

a["CONSTANT"] = 23;


Beware of this behavior of ActionScript when using consts in your code!


Declaring Constants in Java/JavaScript/ActionScript

This has confused me some times in the past, so I thought I’d put it here for easy reference.


final int VALUE = 10;


const VALUE = 10;


const VALUE:Number = 10;


The point of confusion is that const is a reserved word in Java but is not used for declaring constants (yet!). Java uses the final keyword for this purpose.