
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Creating Variables (Declaration)
|
39
state, a variable contains a special value called undefined (indicating the absence of
data).
To declare a new variable, we use the var statement. For example:
var speed;
var bookTitle;
var x;
The word var tells the interpreter that we’re declaring a variable, and the moniker
that follows, such as
speed, bookTitle,orx, becomes our new variable’s name. We
can create variables anywhere we can attach code: on a keyframe, a button, or a
movie clip.
If you enter code in the Actions panel while on a frame that is not a
keyframe, the code is attached to the nearest preceding keyframe.
We can also declare several variables with one var statement, like this:
var x, y, z;
However, doing so impairs our ability to add comments next to each variable.
Once a variable has been created, we can assign it a value, but before we learn how
to do that, let’s consider some of the subtler details of variable declaration.
Automatic Variable Creation
Many programming languages require you to declare variables before depositing data
into them; failure to do so causes an error. ActionScript is not that strict. If we assign
a value to a variable that does not exist, the interpreter creates a new variable for us.
The bank, to continue that analogy, automatically opens an account when ...