
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
34
|
Chapter 3: Datatypes and Type Checking
Instead of redeclaring a variable’s datatype, you should use two separate variables
when you need to store values of two different datatypes. That said, you can safely
reuse a loop counter when its datatype is the same across multiple loops in the same
script, function, or method. Just remember to omit the keyword var, the colon, and
the datatype on the second and subsequent loops. For example:
// First loop: declare i as a Number.
for (var i:Number = 0; i < 10; i++) {
trace(i);
}
// Second loop: reuse i, but don't include var or :Number.
for (i = 15; i > 0; i--) {
trace(i);
}
In a loop in which a different datatype is required, use a new variable:
// Use p instead of i.
for (var p:String in someObj) {
// List properties of an object.
trace("Found property: " + p);
}
Naturally, local variables within one function have no relation to local variables
within a separate function. So you could have a local variable declared as a number
in one function and a different local variable of the same name declared as a string in
another function.
Declaring Method Parameter and Return Value Datatypes
The following code shows how to declare the datatype of method or function param-
eters and return values:
function methodName (param1Name:param1Type,
param2Name:param2Type ...