Now, we can use the var statement to declare a variable, and stores the values which may get changed during the running time, while we use the const statement to declare a constant, stores the values won’t get changed during the whose application’s life circle.
Assignment Statement
Except the variable and constant value, we also talk about the literal value. And the literal values are use for assigning the values to the variables and constants. Just as the following statement.
- msg = “Hello, ActionScript 3.0″;
This kind of statement is called assignment statement. The left side of the equal sign is usually a variable or constant; the right side of the equal sign is usually a literal value or a variable.
The following code shows how to use assignment statement.
- var i:int; // Declare an int type variable i
- i = 20; // Assign the literal value 20 to i
- var j:int; // Declare an int type variable i
- j = i; // Assign i to j
- var k:int = 30; //Combine the declaration and assignment together
- var a:int = 1, b:int = 2, c:int = 3; // Declare three variables and assign values to each one.
Default values of variables
Now, you know how to use the assignment statement to assign the variables. But what if you forget assigning a value to the variable, and what will happen then?
Actually, if you just declare a variable, but not assign any values to it. The variable will get a default value according to its type.
Let’s try the following code.
- var i:int;
- trace(i);
- var j:uint;
- trace(j);
- var k:Number;
- trace(k);
- var flag:Boolean;
- trace(flag);
- var msg:String;
- trace(msg);
If you use the flash cs3 authoring tools, you will get the following result in the output panel. If you use the flex builder, see the console.
Note: You can consider trace() is a debug function. Eh, maybe you don’t know what is debug function, just remember that when you put something into trace(), such as a variable or constant, you can see the value of the variable or constant in the output panel in flash authoring tools or in the console in flex builder. For example, when we use trace(i), the output panel shows 0 in the above image;
The values in the output panel are the default values for the corresponding type. You can take a look at the following table for detail.
|
Data type |
Default value |
|
int |
0 |
|
uint |
0 |
|
Number |
|
|
Boolean |
false |
|
String |
null |
|
untyped |
Undefined |
|
All other classes |
null |
Number and NaN
NaN refers to Not a Number. This value is not only the default value for variables of type Number, but also as the result of any operation that should return a number but does not. For example, if you attempt to calculate the square root of a negative number, the result will be NaN. Other special Number values include positive infinity and negative infinity.
Note: The result of division by 0 is only NaN if the divisor is also 0. Division by 0 produces infinity when the dividend is positive or -infinity when the dividend is negative.
NaN is use for represent a value that doesn’t exist. So, if you try to compare two “NaN” variables, you will get false.
If you want to know whether a number type variable is NaN or not, you can’t just simple compare the variable to NaN, you should use the isNaN() function.
Be careful with the NaN, and also the other values. Here, I suggest you assign the variables when you declare them. This just needs only a slight effort, but you’ll get much payback from it.
When you declare one, assign one.

March 17th, 2009
Ntt.cc 








Posted in
Tags: 
RSS Feed
Email Feed
[...] Here and Here. [...]