24
Chapter 1: ActionScript Basics
•
“Casting objects” on page 25
•
“Determining an item’s data type” on page 26
Automatic data typing
If you do not explicitly define an item as holding either a number, a string, or another data type,
Flash Player will, at runtime, try to determine the data type of an item when it is assigned. If you
assign a value to a variable, as shown in the following example, Flash Player evaluates at runtime
the element on the right side of the operator and determines that it is of the Number data type:
var x = 3;
A later assignment might change the type of
x
; for example, the statement
x = "hello"
changes
the type of
x
to a string. Because
x
was not declared using strict data typing, the compiler cannot
determine the type; to the compiler, the variable
x
can have a value of any type. (See
“Strict data
typing” on page 24
.)
ActionScript converts data types automatically when an expression requires it and the variables
aren’t strictly typed.
For example, when you pass a value to the
trace()
statement,
trace()
automatically converts
the value to a string and sends it to the log file.
In expressions with operators, ActionScript converts data types as needed; in the following
example, when used with a string, the addition (
+)
operator expects the other operand to be
a string:
"Next in line, number " + 7
ActionScript converts the number 7 to the string
"7"
and adds it to the end of the first string,
resulting in the following string:
"Next in line, number 7"
Strict data typing is recommended; for more information, see
“Strict data typing” on page 24
.
Strict data typing
ActionScript lets you explicitly declare the object type of a variable when you create it, which is
called
strict data typing
. Strict data typing offers several benefits at compile time. Because data type
mismatches trigger compiler errors, strict data typing helps you find bugs in your code at compile
time and prevents you from assigning the wrong type of data to an existing variable. During
authoring, strict data typing activates code hinting in the ActionScript editor (but you should still
use instance-name suffixes for visual elements). Although strict data typing is relevant only at
compile time, it can increase performance at runtime by making your scripts run faster.
To assign a specific data type to an item, specify its type using the
var
keyword and post-colon
syntax, as shown in the following example:
// strict typing of variable or object
var x:Number = 7;
var birthday:Date = new Date();
// strict typing of parameters
Содержание FLEX
Страница 1: ...Flex ActionScript Language Reference ...
Страница 8: ......
Страница 66: ...66 Chapter 2 Creating Custom Classes with ActionScript 2 0 ...
Страница 76: ......
Страница 133: ...break 133 See also for for in do while while switch case continue throw try catch finally ...
Страница 135: ...case 135 See also break default strict equality switch ...
Страница 146: ...146 Chapter 5 ActionScript Core Language Elements See also break continue while ...
Страница 229: ...while 229 i 3 The following result is written to the log file 0 3 6 9 12 15 18 See also do while continue for for in ...
Страница 808: ...808 Chapter 7 ActionScript for Flash ...
Страница 810: ...810 Appendix A Deprecated Flash 4 operators ...
Страница 815: ...Other keys 815 Num Lock 144 186 187 _ 189 191 192 219 220 221 222 Key Key code ...
Страница 816: ...816 Appendix B Keyboard Keys and Key Code Values ...
Страница 822: ...822 Index ...