Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Data and Data Types > About data types > About type checking | |||
Type checking refers to verifying that the type of a variable and an expression are compatible. Therefore, Flash checks that the type you specify for a variable matches the value(s) that you assign to it. For more information on strict data types and assigning data types, see About assigning data types and strict data typing and Assigning a data type.
Type checking can occur at either compile time or runtime. If you use strict data typing, type checking occurs at compile time. Because ActionScript is a dynamically typed language, ActionScript can also type checking at runtime.
For example, the following code does not specify the data type of the parameter xParam. At runtime, you use the parameter to hold a value of type Number and then a value of type String. The dynamicTest() function then uses the typeof operator to test whether the 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() that invokes a method and returns a property, neither of which is 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;
}
Flash CS3
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000652.html
Comments
Fumio Nonaka said on Apr 19, 2007 at 4:36 PM :