Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > ActionScript

  • Print
  • + Share This
This chapter is from the book

Defining Constants

Many ActionScript developers have been resourceful in finding ways to create workarounds and build functionality that wasn’t intended by the language’s creators. For example, we’ve all created custom constants to store important data for use throughout an application. But custom constants are no longer necessary in ActionScript. At last, the const keyword lets you define a property that cannot be altered, remaining the same in all situations.

AS2: Defining Constants

In ActionScript 2 there was no true way to define constants. Developers would often resort to creating static variables as constants that they could add to a class in order to keep them easily accessible.

 1  class Status
 2  {
 3       static public var PASSED:Number = 1;
 4       static public var COMPLETE:Number = 2;
 5       static public var FAILED:Number = 3;
 6       static public var INCOMPLETE:Number = 4;
 7  }

Allowing an external class to change the values of your constants can lead to bugs that are very hard to find. Plus, it’s completely insecure: a knowledgeable user with malicious intentions could change the constants and break your code.

AS3: Defining Constants

In ActionScript 3 you no longer have to write custom code for constants, as in AS2. Now you can use the const keyword to define a property as a constant, so that in all situations the value remains the same and is reliable.

  • + Share This
  • 🔖 Save To Your Account