In the preceding section we learned that when an expression is cast to a primitive type to which it does not belong, then that expression is converted to the specified type. For example, consider the following code, which casts a Date object to the primitive datatype Boolean:
Because Boolean is a
primitive type, and the Date
object does not belong to the Boolean type, ActionScript converts the
Date object to the Boolean type. The result is the Boolean value
true (see Table 8-5).
Cast operations are sometimes used not to tell the compiler the type of a given expression but to convert that expression to a primitive datatype.
A cast operation can convert any value to a particular primitive type.
For example, the following code converts a floating-point number (a number with a fractional value) to an integer (a number with no fractional value):
The result of the preceding cast operation is the integer 4.
Likewise, the following code converts the Boolean value
true to the integer 1, and the Boolean value
false to the integer 0:
int(true); // Yields 1 int(false); // Yields 0
The preceding technique might be used to reduce the size of data transmitted to a server when submitting a series of Boolean options.
Table 8-1 shows the results of converting various datatypes to the Number type.
Table 8-1. Conversion to Number
Result after conversion