
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
40
|
Chapter 3: Datatypes and Type Checking
Compatibility with undefined also allows ActionScript to assign undefined to param-
eters, variables, and properties that have never been assigned a value. For example,
here we define a method that displays a message on screen, complete with the mes-
sage sender’s name. If the name of the message sender is not provided, ActionScript
sets the
sentBy parameter to undefined, in which case the method uses the name
“Anonymous”:
public function displayMsg (msg:String, sentBy:String):Void {
// Use "Anonymous" if a name was not supplied.
if (sentBy == undefined) {
sentBy = "Anonymous";
}
// Display the message in a text field.
output.text = sentBy + ": " + msg;
}
Built-in Dynamic Classes
We saw earlier that trying to dynamically add a property to the Date class generates a
compile-time error in ActionScript 2.0. To allow new properties and methods to be
added to a class’s instances without generating a compile-time error, we can use a
dynamic class. You can define your own dynamic classes, but some built-in classes
are dynamic by default. Due partially to the architecture of the Flash Player and par-
tially to the heritage of ActionScript 1.0, the following native ActionScript 2.0 classes
are dynamic:
• Array
• ContextMenu
• ContextMenuItem
• Function
•