For example, the following code does not specify the data type of the parameter
runtime, you use the parameter to hold a value of type Number and then a value of type
. The
String
dynamicTest()
parameter is of type String or Number.
function dynamicTest(xParam) {
if (typeof(xParam) == "string") {
var myStr:String = xParam;
trace("String: " + myStr);
} else if (typeof(xParam) == "number") {
var myNum:Number = xParam;
trace("Number: " + myNum);
}
}
dynamicTest(100);
dynamicTest("one hundred");
You do not need to explicitly add data type information in your ActionScript. The
ActionScript compiler lets you use properties and invoke methods that do not exist at compile
time. This lets you create properties or assign dynamically methods at runtime.
An example of the flexibility afforded by dynamic type checking involves the use of properties
and methods that are not known at compile time. Because the code is less restrictive, it can
lead to benefits in some coding situations. For example, the following code creates a function
named
runtimeTest()
known to the compiler. The code will not generate a compile-time error, but if the property or
method is not accessible at runtime, then a runtime error will occur.
function runtimeTest(myParam) {
myParam.someMethod();
return myParam.someProperty;
}
About determining data type
While testing and debugging your programs, you might discover problems that seem to be
related to the data types of different items. Or if you use variables that are not explicitly
associated with a data type, you might find it useful to know the data type of a given variable.
Using ActionScript, you can determine an item's data type. You can use the
to return information about data.
Use the
operator to get the data types, but remember that
typeof
information about the class to which an instance belongs.
function then uses the
that invokes a method and returns a property, neither of which is
operator to test whether the
typeof
typeof
About data types
. At
xParam
operator
typeof
does not return
85
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?