As3 beginner's note: ActionScript-Variables

Source: Internet
Author: User
Tags define local
Variable

Variables can be used to store values used in programs. To declare a variable, you must use the VaR statement and the variable name together. In ActionScript 2.0, only when you use a class
You must use the VaR statement. In ActionScript 3.0, you always need to use the VaR statement. For example, the following ActionScript line declares a name
For I:

VaR I;

If the VaR statement is omitted when a variable is declared, a compiler error occurs in strict mode and a runtime error occurs in standard mode. For example, if
Define variable I, the following code line will produce an error:

 i; // error if i was not previously defined

To associate a variable with a data type, you must perform this operation when declaring the variable. It is legal to declare a variable without specifying the variable type, but this is in the strict Mode
A compiler warning is generated. You can specify the variable type by appending a colon (:) followed by the variable type after the variable name. For example, the following code declares
Int type variable I:

 var i:int;

You can use the value assignment operator (=) to assign values to variables. For example, the following code declares a variable I and assigns the value 20 to it:

 var i:int;  i = 20;

You may find it easier to assign values to variables while declaring variables, as shown in the following example:
 var i:int = 20;
Usually, the method of assigning values to variables while declaring variables is not only common when assigning primitive values (such as integers and strings), but also when creating arrays or instantiating classes
Examples are also very common. The following example shows an array using a line of code declaration and value assignment.

 var numArray:Array = ["zero", "one", "two"];

You can use the new operator to create a class instance. The following example creates an instance named customclass and assigns
Instance reference:
 var customItem:CustomClass = new CustomClass();

To declare multiple variables, you can use the comma (,) operator to separate the variables and declare all these variables in a line of code. For example, the following code is
Declare three variables in the Code:
 var a:int, b:int, c:int;

You can also assign values to each variable in the same line of code. For example, the following code declares three variables (A, B, and C) and assigns values to each variable:

 var a:int = 10, b:int = 20, c:int = 30;

Although you can use the comma operator to combine the declaration of each variable into a statement, this may reduce the readability of the Code.

Understand the scope of Variables

The range of variables refers to the code area in which variables can be accessed through word reference. Global variables are defined in all areas of the Code, while local variables are
Variable defined only in a part of the code. In ActionScript 3.0, variables are always assigned with scopes that declare their functions or classes. The global variable is
The external variables defined by the function or class. For example, the following code creates the variable by declaring a global variable named strglobal outside any function.
Quantity. This example shows that global variables can be used both inside and outside the function definition.

 var strGlobal:String = "Global";  function scopeTest()  {  trace(strGlobal); // Global  }  scopeTest();  trace(strGlobal); // Global

You can declare a variable as a local variable within the function definition. The minimum code area that can define local variables is the function definition. The
Local variables exist only in this function. For example, if a variable named str2 is declared in a function named localscope (), the variable will not be available outside the function.
.

 function localScope()  {  var strLocal:String = "local";  }  localScope();  trace(strLocal); // error because strLocal is not defined globally
If the name of a local variable has been declared as a global variable, when the local variable is in the scope, the local definition will hide (or mask) The global definition.
Global variables still exist outside the function. For example, the following code creates a global string variable named str1, and then creates
Local variables with the same name. The trace statement in the function outputs the local value of the variable, while the trace statement outside the function outputs the global value of the variable.

 var str1:String = "Global";  function scopeTest ()  {  var str1:String = "Local";  trace(str1); // Local  }  scopeTest();  trace(str1); // Global

Unlike variables in C ++ and Java, actionscript variables do not have block-level scopes. A code block is an arbitrary block between the left braces ({) and the Right braces (}).
A group of statements. In some programming languages (such as C ++ and Java), variables declared inside the code block are unavailable outside the code block. This restriction on scope is called
It is block-level scope, and there is no such restriction in ActionScript. If you declare a variable in a code block, this variable is not only available in this code block
And is available in any other part of the function to which the code block belongs. For example, the following functions contain variables defined in different block scopes. All variables
All are available in the entire function.
 function blockTest (testArray:Array)  {  var numElements:int = testArray.length;  if (numElements > 0)  {  var elemStr:String = "Element #";  for (var i:int = 0; i < numElements; i++)  {  var valueStr:String = i + ": " + testArray[i];  trace(elemStr + valueStr);  }  trace(elemStr, valueStr, i); // all still defined  }  trace(elemStr, valueStr, i); // all defined if numElements > 0  }    blockTest(["Earth", "Moon", "Sun"]);

Interestingly, if there is no block-level scope, you can read and write the variable before it is declared before the function ends. This is because it exists.
A method called lift, which indicates that the compiler moves all variable declarations to the top of the function. For example, the following code will be compiled, even if there is num first
The same is true if the num variable is declared after the initial trace () function of the variable:

 trace(num); // NaN  var num:Number = 10;  trace(num); // 10

However, the compiler will not upgrade any value assignment statement. This explains why the initial trace () of num generates Nan (rather than a number), and Nan is
The default value of the number data type variable. This means that you can even assign values to variables before declaring them, as shown in the following example:

 num = 5;  trace(num); // 5  var num:Number = 10;  trace(num); // 10
Default ValueThe default value is the value included in the variable before setting the value of the variable. The value of the variable set for the first time is actually the initialization variable. If you declare a variable but do not set
The variable is in the uninitialized state. The value of an uninitialized variable depends on its data type. The following table describes the default values of variables and
Type to organize these values:

Data Type Default Value
Boolean False
Int 0
Number Nan
Object Null
String Null
Uint 0
Not declared (equivalent to Type comment) Undefined
All other classes (including user-defined classes ). Null
For a variable of the number type, the default value is Nan (rather than a number), Nan is a special value defined by the IEEE-754 standard, which represents a non-numeric
A value.
If you declare a variable but do not declare its data type, the default data type * will be applied, which actually indicates that the variable is a non-type variable. If you do not have
If no type variable is initialized with a valid value, the default value of this variable is undefined.
For data types other than Boolean, number, Int, and uint, the default values of all uninitialized variables are null. This applies to
All the defined classes and all the custom classes you have created.
For Boolean, number, Int, or uint type variables, null is not a valid value. If you try to assign null to a variable like this, the value is converted
The default value of the data type. A null value can be assigned to an object-type variable. If you try to assign the value undefined to a variable of the object type
The value is converted to null.
For a number variable, there is a special top-level function named isnan (). If the variable is not a number, this function returns a Boolean value of true. Otherwise
Returns false.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.