Flashmech.net

codes, tips, mac, softwares and deals

Flashmech.net

Conversion to Primitive Types

March 24th, 2008 · 2 Comments · ActionScript, Books

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 :P :

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. :D

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 DataResult after conversion
undefinedNaN (the special numeric value "Not a Number", which represents invalid numeric data).
null0
intThe same number
uintThe same number
Boolean1 if the original value is true; 0 if the original value is false
Numeric stringEquivalent 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 string0
"Infinity"Infinity
"-Inifinity"-Infinity
Other stringsNaN
ObjectNaN

Table 2: Conversion to int

Shows the results of converting various datatypes to the int type.
Original DataResult after conversion
undefined0
null0
Number or uintAn 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
Boolean1 if the original value is true; 0 if the original value is false
Numeric stringEquivalent numeric value, converted to signed-integer format
Empty string0
"Infinity"0
"-Inifinity"0
Other strings0
Object0

Table 3: Conversion to uint

Shows the results of converting various datatypes to the uint type.
Original DataResult after conversion
undefined0
null0
Number or intAn 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
Boolean1 if the original value is true; 0 if the original value is false
Numeric stringEquivalent numeric value, converted to unsigned-integer format
Empty string0
"Infinity"0
"-Inifinity"0
Other strings0
Object0

Table 4: Conversion to String

Shows the results of converting various datatypes to the String type.
Original DataResult 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 valueString equivalent of the number. For example, 944.345 becomes "944.45".
ObjectThe 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 DataResult after conversion
undefinedfalse
nullfalse
NaNfalse
0false
Infinitytrue
-Infinitytrue
Other numeric valuetrue
Nonempty stringtrue
Empty string ("")false
Objecttrue

DeliciousStumbleUponDiggTwitterMixxTechnoratiFacebookNews VineRedditLinkedInEmail

Tags:

2 Comments so far ↓

  • Steven Sacks

    Instead of a ternary operation (a ? b : c), you can just say

    var flag:Boolean = (a == 0);

    or

    var flag:Boolean = (a == "hello");

    This works really well for things like

    private function onButtonClick(e:Event):void
    {
        var i:int = buttons.length;
        while (i--)
        {
            e.currentTarget.selected = e.currentTarget == buttons[i];
        }
    }
  • flashmech

    I love that example! Thanks! :D