Datatype Conversion Rules

     

When the ActionScript interpreter expects one datatype, and you use a different datatype, the interpreter creates a new value of the expected type. For instance, suppose your program contains the following statements:

 var x; x = 63  "my dentist"; trace(x); // ?? 

What will the trace statement display?

If you ask the ActionScript interpreter to subtract a string from a number, it attempts to comply by implicitly converting the string to a number and then subtracting. In this case, the string "my dentist" cannot be successfully converted to a number. It was for cases such as this that the NaN value was created. The ActionScript interpreter converts "my dentist" to NaN , which is officially of the number datatype. Any mathematical operation involving NaN also results in NaN . Therefore, x is equal to NaN .

The same thing happens if you use strong data typing on x and cast "my dentist" to a number:

 var x:Number; x = 63  Number("my dentist"); trace(x); // NaN 

In this case, the datatype conversion didn't do much good, except that it allowed the program to continue functioning (the alternative to implicit conversion being an error, as in Java). Then again, you gave the ActionScript interpreter an impossible job. On the other hand, if you give it a string such as "3" that can be converted to a number, the ActionScript interpreter does something quite helpful:

 x = 63  "3"; trace(x); // 60 

The result of an implicit datatype conversion is always a primitive datatype: string , number , or Boolean . Table A.1 shows the rules for converting to a number.

Table A.1. Implicit Conversion to a Number

Supplied Value

Value After Conversion

undefined

FP7: NaN , FP6:

null

FP7: NaN , FP6:

Boolean false

Boolean true

1

A numeric string that can be converted to a base-10 number. (Can contain numerals, a decimal point, a plus sign, a minus sign, and whitespace.)

The equivalent numeric value. (Exception: The + operator concatenates all strings, including numeric strings. See Chapter 13.)

A non-numeric string. (Includes the empty string and any string containing an alphabetic character, including strings starting with "x" , "0x" , and "FF" .)

NaN

"Infinity"

Infinity

"-Infinity"

-Infinity

"NaN"

NaN

Any array

NaN

Any object

NaN

Any movie clip

NaN


Table A.2 shows the rules for converting to a string.

Table A.2. Implicit Conversion to a String

Supplied Value

Value After Conversion

undefined

FP7: undefined , FP6: "" (the empty string)

null

"null"

Boolean false

"false"

Boolean true

"true"

NaN

"NaN"

"0"

Infinity

"Infinity"

-Infinity

"-Infinity"

Any other numeric value

The string equivalent of the numeric value.

Any array

A comma-separated list of element values.

Any object

The return value of the object's toString() method. The default is "[object Object]" . The built-in Date object returns a date.

Any movie clip

The absolute path to the movie clip instance; for example, "_level0.myClip" .


Table A.3 shows the rules for converting to a Boolean.

Table A.3. Implicit Conversion to a Boolean

Supplied Value

Value After Conversion

undefined

false

null

false

NaN

false

false

Infinity

true

-Infinity

true

Any other numeric value

true

Empty string

false

Any string that can be converted to a valid nonzero number

true

Any string that cannot be converted to a valid nonzero number. (Departs from ECMA-262 standard to maintain compatibility with Flash 4.)

false

Any array

true

Any object

true

Any movie clip

true


You can explicitly convert any datum to a number, string, or Boolean.

Converting to a Number

You convert to a number by using the global Number() function. For instance, if a user types his or her age into an input text box, it is initially a string. You convert it to a number like this:

 user1Age = Number(user1Age); 

If the user types in "23" , the interpreter sees the preceding line as follows :

 user1Age = Number("23"); 

The interpreter sets the user1Age variable to the number 23.

The Number() function assumes decimal numbers with optional trailing exponents:

 Number("2.637e-4"); // 0.0002637 

You can use hexadecimal numbers if you prefix the string with 0x , as in this instance:

 greenColor = "0x00FF00"; // hex representation of green color, a string hexGreen = Number(greenColor); // 65280, the decimal form of 0x00FF00 

The parseInt() and parsefloat() functions also convert strings to numbers, but the string must have the following format: The first nonblank character must be the first character of the number you want, and the first character after the number you want must be non-numeric. The following examples use parseInt() :

 1: parseInt("12years") ; // extracts 12 2: parseInt("   12years") ; // extracts 12 3: parseInt("12 years") ; // extracts 12 4: parseInt("12 1 year olds") ; // extracts 12 5: parseInt("twelve 1 year olds") ; // NaN 

Looking at line 1, the first character after the number is y , which is non-numeric. Line 2 shows that leading blank characters don't matter. Lines 3 and 4 demonstrate that a following blank character counts as non-numeric. In line 5, the first non-blank character is not the first character of a number; therefore, NaN is extracted from this string.

The parseInt() function can also take a second argument that specifies the radix (base) of the result. For instance:

 parseInt("10",16); // extracts 16  hex 10 (base 16) parseInt("10",8); // extracts 8  octal 10 (base 8) parseInt("10",10); // extracts 10  decimal 10 (base 10) 

parseInt() and Hexadecimal Numbers

parseInt() always assumes that numbers starting with 0x are hexadecimal:

 parseInt("0x10"); // 16 

Trying to override such an implied hexadecimal yields results that aren't usually useful:

 parseInt("0x10", 10); // 0 parseInt("0x10", 8); // 0 


Numbers starting with (but not with 0x ) are octal by default:

 parseInt("010"); // 8 

Unlike with the hexadecimal 0x , you can override the implied octal radix and get useful results:

 parseInt("010", 10); // parsed as decimal, equals 10 

Don't forget the quotation marks around the string. Sometimes, you can get by with leaving them off, but not always:

 trace(parseInt(010)); // 0  not what you want !!! 

The following examples show how to use parseFloat() :

 trace(parseFloat("12.5years")) ; // extracts 12.5 trace(parseFloat("   12.5years")) ; // extracts 12.5 trace(parseFloat("12.5 years")) ; // extracts 12.5 trace(parseFloat("12.5 1 year olds")) ; // extracts 12.5 trace(parseInt("twelve and a half 1 year olds")) ; // NaN 

The last example is NaN because the first nonblank character is not numeric.

Converting to a String

You can convert to a string by using either the toString() method or the String() global function. By default, the two accomplish the same thing, though they use different syntax:

 var x = 6; x.toString(); // "6" String(x); // "6" 

Although methods belong only to objects, implicit datatype conversion allows the toString() method to work with any datatype, according to the rules in Table A.2:

 var x = 6; trace(x.toString()); // "6" y = true; trace(y.toString()); // "true" z = null; trace(z.toString()); // "undefined" trace(Math.toString()); // "[object Object]" - Math is a built-in object 

The toString() method has an optional argument that sets the radix (base) of the result, if the supplied datum is a number. For instance, if the number and the radix are the same (such as the number 6 in base 6, the number 7 in base 7), the result is "10" :

 trace(x.toString(x)); // "10"  true if x is any number 

The following is another example. Note the parentheses required around the number 65280 :

 (65280).toString(16); // "ff00" 

Using Implicit Datatype Conversion

You can use a couple of tricks to take advantage of implicit datatype conversion. Their advantage is their very concise syntax. On the downside, when you're reading the code, what they do may not be intuitively obvious.

For instance, the "add and reassign " operator ( += ) can accomplish the same thing as the toString() method or the String() global function. It's more obvious with toString() or String() that you are converting to a string, but the "add and reassign" operator is more concise.

Any datum is converted to a string, following the rules of Table A.2, if you add the empty string to it. For example, the second line here accomplishes the same thing as String() or toString() :

 x = 77; // x is a number x += ""; // x is now a string: "77" 

"Add and Reassign" and "Subtract and Reassign"

The "add and reassign" operator ( += ) adds a second operand to a first operand and assigns the result to the first operand. For example, x += "" is the equivalent of x = x + "" .

The "subtract and reassign" operator ( -= ) subtracts a second operand from a first operand and assigns the result to the first operand. For example, x -= 0 is the equivalent of x = x - 0 .


Similarly, subtracting 0 from any datum converts it into a number. For instance:

 x = new Object(); // x is an object x -= 0; // x is a number: NaN 



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net