When I first determined to datatype my codes strictly, I had this illusion that I must assign everything according to their data types. Here’s an example of me being disillusioned
:
var age:uint = 2; // hardcoded value for testing
var isBorn:Boolean = ( age == 0 ) ? false : true;
trace( isBorn ); // returns true.
age = 0;
isBorn = ( age == 0 ) ? false : true;
trace( isBorn ); // returns false.
As you can see from the above example, it was not pretty. It was only when I read the section of Conversion to Primitive Types in Essential ActionScript 3.0 did it dispels my misconception.
So here’s what I can actually do:
var age:uint = 2;
var isBorn:Boolean = age;
trace( isBorn ); // returns true as well.
age = 0;
isBorn = age;
trace( isBorn ); // returns false. Works well!
In another typical scenario, if I wanted to make a movieclip’s visibility to false and alpha to 0, instead of doing:
mc.visible = false;
mc.alpha = 0;
I can just:
mc.visible = mc.alpha = 0;
** Do you see the advantage already? **
More info about type conversions can be found here on Adobe LiveDocs.
The information below is referenced from: Essential ActionScript 3.0 by Colin Moock. Copyright 2007 O’Reilly Media, Inc., 0-596-52694-6
Table 1: Conversion to Number
Shows the results of converting various datatypes to the Number type.
| Original Data |
Result after conversion |
| undefined |
NaN (the special numeric value "Not a Number", which represents invalid numeric data). |
| null |
0 |
| int |
The same number |
| uint |
The same number |
| Boolean |
1 if the original value is true; 0 if the original value is false |
| Numeric string |
Equivalent numeric value if string is composed only of base-10 or base-16 numbers, whitespace, exponent, decimal point, plus sign, or minus sign (e.g., "-1.485e2" becomes -148.5) |
| Empty string |
0 |
| "Infinity" |
Infinity |
| "-Inifinity" |
-Infinity |
| Other strings |
NaN |
| Object |
NaN |
Table 2: Conversion to int
Shows the results of converting various datatypes to the int type.
| Original Data |
Result after conversion |
| undefined |
0 |
| null |
0 |
| Number or uint |
An integer in the range -231 through 231-1, out of range values are brought into range using the algorithm listed in section 9.5 of the Standard ECMA-262, Third Edition |
| Boolean |
1 if the original value is true; 0 if the original value is false |
| Numeric string |
Equivalent numeric value, converted to signed-integer format |
| Empty string |
0 |
| "Infinity" |
0 |
| "-Inifinity" |
0 |
| Other strings |
0 |
| Object |
0 |
Table 3: Conversion to uint
Shows the results of converting various datatypes to the uint type.
| Original Data |
Result after conversion |
| undefined |
0 |
| null |
0 |
| Number or int |
An integer in the range 0 through 231-1, out of range values are brought into range using the algorithm listed in section 9.6 of the Standard ECMA-262, Third Edition |
| Boolean |
1 if the original value is true; 0 if the original value is false |
| Numeric string |
Equivalent numeric value, converted to unsigned-integer format |
| Empty string |
0 |
| "Infinity" |
0 |
| "-Inifinity" |
0 |
| Other strings |
0 |
| Object |
0 |
Table 4: Conversion to String
Shows the results of converting various datatypes to the String type.
| Original Data |
Result after conversion |
| undefined |
"undefined" |
| null |
"null" |
| Boolean |
"true" if the original value was true; "false" if the original value was false |
| NaN |
"NaN" |
| 0 |
"0" |
| Infinity |
"Infinity" |
| -Infinity |
"-Infinity" |
| Other numeric value |
String equivalent of the number. For example, 944.345 becomes "944.45". |
| Object |
The value that results from calling toString() on the object. By default, the toString() method of an object returns "[object className]", where className is the object's class. The toString() method can be overridden to return a more useful result. For example, toString() of a Date object returns the time in human-readable format, such as: "Sun May 14 11:38:10 EDT 2000"), while toString() of an Array object returns comma-separated list of element values. |
Table 5: Conversion to Boolean
Shows the results of converting various datatypes to the Boolean type.
| Original Data |
Result after conversion |
| undefined |
false |
| null |
false |
| NaN |
false |
| 0 |
false |
| Infinity |
true |
| -Infinity |
true |
| Other numeric value |
true |
| Nonempty string |
true |
| Empty string ("") |
false |
| Object |
true |