Just another weblog

Declaring Variable & Function In Action Script 2.0 (Flash)

To further aide debugging and assurance that you are in fact using your variables as you had intended for them to be used, ActionScript 2.0 has introduced strict data typing to the language. What this is, is the ability to specify what type a certain variable is when it’s created (Number, String, MovieClip, etc.). Then, should you ever attempt to try to use it as a variable of an alternative type, an error will occur thereby isolating a confliction helping you to debug.

Strict data typing also allows for Flash to associate a variable with an object to help you with code hints when using that object. Before, this was only possible using underscore suffixes in your variable naming. Now you have the option of using strictly typed variables instead, allowing for more room in your own naming conventions. (Note: XML definitions will still be needed for code hints to function properly. :)

Typing is always placed following a variable or function declaration. For variables, the type is placed immediately after the variable name separated by a colon (:). For functions, placement directly after the parameters list, also separated by a colon. Function types here, however, refer to the type of the value that the function is returning. If the function has no return, Void is used. Also, for functions, the parameters used can too be strictly typed.

1:var variableName:Type = value;

2:function functionName(parameter:Type):returnType {}

The following is an example of some variables and a function being strictly typed. Notice that when a type is given a value not matching its data type, an error occurs.