
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
32
|
Chapter 3: Datatypes and Type Checking
• If a value of an incompatible type is stored in a variable, passed as a function
parameter, or returned by a function, the compiler generates a type mismatch
error.
• If a nonexistent property or method is accessed through a typed variable, func-
tion parameter, or function return value, the compiler generates an error explain-
ing that the property or method cannot be found.
The compiler selectively type checks only items that have their types declared. Unfor-
tunately, Flash MX 2004 does not provide a way to force the ActionScript compiler
to report which items have no datatype declared. Hence, type errors can slip through
if you’re not vigilant about declaring datatypes. (For comparison, in Java, a program
will not compile if any datatype declarations are missing.)
Declaring Variable and Property Datatypes
To declare the datatype of a variable or property, use the following syntax:
var variableOrPropertyName:datatype;
where variableOrPropertyName is the name of the variable or property, and datatype
is the type of data that the variable or property can legally contain. For example, the
following code creates a variable called
currentTime and declares its datatype as
Date. It then assigns a new Date instance to the variable, which is legal, ...