About variables
91
When you assign a value, you use an operator to define a value to a variable. For example, the
following script uses the assignment operator to assign a value of 7 to the variable
numChildren
:
var numChildren:Number = 7;
If you want to change the value of the
numChildren
variable, use the following code:
numChildren = 8;
For more information on using operators in your ActionScript, see
“About operators”
on page 176
.
About naming variables
Be careful when you start naming variables, because although they can have nearly any name,
there are some rules. A variable’s name must follow these rules:
■
A variable must be an identifier.
■
A variable cannot be a keyword or an ActionScript literal such as
true
,
false
,
null
, or
undefined
. For more information on literals, see
“About literals” on page 130
.
■
A variable must be unique within its scope (see
“About variables and scope” on page 96
).
■
A variable should not be any element in the ActionScript language, such as a class name.
If you don’t follow the rules when you name a variable, you might experience syntax errors or
unexpected results. In the following example, if you name a variable
new
and then test your
document, Flash will generate a compiler error:
// This code works as expected.
var helloStr:String = new String();
trace(helloStr.length); // 0
// But if you give a variable the same name as a built-in class...
var new:String = "hello"; //error: Identifier expected
var helloStr:String = new String();
trace(helloStr.length); // undefined
NO
TE
You don’t need to use
var
because the variable has previously been defined.
NO
TE
An
identifier
is the name of a variable, property, object, function, or method. The first
character of an indentifier must be a letter, underscore (_), or dollar sign ($). Each
subsequent character can be a number, letter, underscore, or dollar sign.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...