PDA

View Full Version : Constants in ActionScript



foodpk
April 30th, 2005, 12:36 PM
You know, I've always wondered how to make constants in ActionScripts. For instance, in some languages when you write


const CUBE_SIDES = 6;

then whenever you would use CUBE_SIDES in the code, it would interpret it as 6. The trick is that you can't change a constant, so you couldn't do it by accident.
How to do that with ActionScript? The only way I know is that you store your constants as variables in the _global object.


_global.CUBE_SIDES = 6;

But then you could change CUBE_SIDES if you wanted to.
Catch my drift?

kode
April 30th, 2005, 01:45 PM
I can think of a couple of options...

You could define it as a read-only property:

this.addProperty("CUBE_SIDES", function(){return 6;}, null);
trace(CUBE_SIDES); // 6
CUBE_SIDES = 8;
trace(CUBE_SIDES); // Its value is still 6
Or define it as a static variable of a class:

class Cube {
static var CUBE_SIDES = 6;
}
Both methods would prevent the value of the variable from being changed. :)