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

Java:

final int VALUE = 10;

JavaScript:

const VALUE = 10;

ActionScript:

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.

Flex Builder 3.2 and AIR projects

I was facing problem running AIR projects since morning. Reinstalled Flex builder 3.0.2 a couple of times. I thought it was an issue related to upgrading to Flash Player 10. However, I later found (through google) that to make the older AIR projects run with the new release of Flex builder, you need to change the line

<application xmlns="http://ns.adobe.com/air/application/1.0">

In app.xml to

<application xmlns="http://ns.adobe.com/air/application/1.5">