ActionScript 3 Language Specification |
|||
| ActionScript 3.0 Language Specification > 6 Types > 6.2 Type Conversions | |||
A type conversion is the translation of a value to a value that is a member of a specific destination type. When the original value is a member of the destination type, the value is unchanged. We call this an identity conversion.
Type conversions occur at runtime in various contexts:
as operatorThe result of the conversion depends on the context of the expression that yields the value to be converted:
var x : T = v
|
Implicit conversion to |
var y : T = v as T
|
|
var z : T = v + 10
|
Conversion according to the rules of the operator |
Implicit conversions occur when a value is assigned to a property, passed as an argument to a function, or returned from a function.
When the destination type is a user-defined type T, an implicit conversion will succeed if the value is an instance of a class that is T or is derived from T. If an implicit conversion does not succeed, then a type error is thrown.
When the destination type is a primitive type, the implicit conversion is described by the corresponding abstract procedure (such as toString() and toNumber().) The following table shows some implicit conversion results:
|
Value |
String |
Number |
int |
uint |
Boolean |
Object |
|---|---|---|---|---|---|---|
|
{} |
"[object Object]" |
NaN |
0 |
0 |
true |
{} |
|
"string" |
"string" |
NaN |
0 |
0 |
true |
"string" |
|
"10" |
"10" |
10 |
10 |
10 |
true |
"10" |
|
null |
Null |
0 |
0 |
0 |
false |
null |
|
undefined |
Null |
NaN |
0 |
0 |
false |
null |
|
true |
"true" |
1 |
1 |
1 |
true |
true |
|
false |
"false" |
0 |
0 |
0 |
false |
false |
|
0 |
"0" |
0 |
0 |
0 |
false |
0 |
|
1 |
"1" |
1 |
1 |
1 |
true |
1 |
|
-1 |
"-1" |
-1 |
-1 |
2E+32-1 |
true |
-1 |
|
1.23 |
"1.23" |
1.23 |
1 |
1 |
true |
1.23 |
|
-1.23 |
"-1.23" |
-1.23 |
-1 |
2E+32-1 |
true |
-1.23 |
|
NaN |
"NaN" |
NaN |
0 |
0 |
false |
NaN |
User-defined types do not have built-in conversion operators, so implicit and explicit conversions behave the same at runtime. Specifically, if a value is not a member of the destination type, then no conversion exists, implicit or explicit, and a runtime exception will result from a cast expression and the default value of the destination type (which is null) will be the result of an as expression.
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/specs/actionscript/3/as3_specification54.html