ActionScript - Data Structures - Using Data Types

Using Data Types

The basic syntax is:

var yourVariableName:YourVariableType = new YourVariableType(Param1, Param2, ..., ParamN);

So in order to make an empty Object:

var myObject:Object = new Object;

Or, in an informal way:

var myObject = {};

Some types are automatically put in place:

var myString:String = "Hello Wikipedia!"; // This would automatically set the variable as a string. var myNumber:Number = 5; // This would do the same for a number. var myObject:Object = {Param1:"Hi!", Param2:76}; //This creates an object with two variables. // Param1 is a string with the data of "Hi!", // and Param2 is a number with the data of 76. var myArray:Array = ; //This is the syntax for automatically creating an Array. //It creates an Array with 3 variables. //The first (0) is a number with the value of 5, //the second (1) is a string with the value of "Hello!", //and the third (2) is an object with {a:5, b:7}.

Unlike some object-oriented languages, ActionScript makes no distinction between primitive types and reference types. In ActionScript, all variables are reference types. However, objects that belong to the primitive data types, which includes Boolean, Number, int, uint, and String, are immutable.

So if a variable of a supposedly primitive type, e.g. an integer is passed to a function, altering that variable inside the function will not alter the original variable, as a new int Object is created when inside the function. If a variable of another (not primitive) datatype, e.g. XML is passed to a function, altering that variable inside the function will alter the original variable as well, as no new XML Object is created.

Some data types can be assigned values with literals:

var item1:String="ABC"; var item2:Boolean=true; var item3:Number=12; var item4:Array=; var item5:Object={name:"Actionscript",version:"3.0"}; var item6:XML = ; //Note that the primitive XML is not quoted

A reference in ActionScript is a pointer to an instance of a class. A reference stores the memory address of an object - operations against references will follow the value of the reference to the memory address of the object & carry out the operation on that object. All objects in ActionScript are accessed through references instead of being accessed directly.

var item1:XML=new XML(""); var item2:XML=item1; item2.firstChild.attributes.value=13; //item1 now equals item2 since item2 simply points to what item1 points to. //Both are now: //

Only references to an object may be removed by using the "delete" keyword. Removal of actual objects and data is done by the Flash Player garbage collector which checks for any existing references in the Flash memory space. If none are found (no other reference is made to the orphaned object), it is removed from memory. For this reason, memory management in ActionScript requires careful application development planning.

var item1:XML=new XML(""); delete item1; //If no other reference to item1 is present anywhere else in the application, //it will be removed on the garbage collector's next pass

Read more about this topic:  ActionScript, Data Structures

Famous quotes containing the words data and/or types:

    To write it, it took three months; to conceive it three minutes; to collect the data in it—all my life.
    F. Scott Fitzgerald (1896–1940)

    The American man is a very simple and cheap mechanism. The American woman I find a complicated and expensive one. Contrasts of feminine types are possible. I am not absolutely sure that there is more than one American man.
    Henry Brooks Adams (1838–1918)