
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Variable Scope
|
47
Determining the Type Manually
Automatic datatyping and conversion can be convenient, but, as Example 2-2 and
Example 2-3 illustrate, they may also produce unexpected results. Before performing
commands that operate on mixed datatypes, you may wish to determine a variable’s
datatype using the typeof operator:
productName = "Macromedia Flash"; // String value
trace(typeof productName); // Displays: "string"
Once we know a variable’s type, we can proceed conditionally. Here, for example,
we check whether a variable is a number before proceeding:
if (typeof age = = "number") {
// okay to carry on
} else {
trace ("Age isn't a number"); // Display an error message
}
For full details on the typeof operator, see Chapter 5.
Variable Scope
Earlier we learned how to create variables and retrieve their values. But all our vari-
ables were attached to a single frame of the main timeline of a Flash document.
When a document contains multiple frames and multiple movie clip timelines, vari-
able creation and value retrieval becomes a little more complicated.
Timeline Variables
To illustrate the use of timeline variables, let’s consider two simple scenarios.
Scenario 1: Accessing a value defined earlier on the same timeline
What happens when we define a variable in one frame of a timeline ...