Every value in ActionScript 3.0 is an object, an instance of some class. The primitive
types, Boolean, int, uint, Number and
String, are stored as unchangeable values without the cost of a full object,
but they still have methods: (255).toString(16) gives "ff".
Everything else is a complex type, held by reference.
All the types
| Type | Holds | Default value |
|---|---|---|
Boolean | true or false | false |
int | A 32-bit signed integer, −2,147,483,648 to 2,147,483,647 | 0 |
uint | A 32-bit unsigned integer, 0 to 4,294,967,295 | 0 |
Number | A 64-bit IEEE 754 double | NaN |
String | A sequence of 16-bit UTF-16 code units | null |
Object | Any object; the root of every class | null |
* | Any value at all, including undefined: the untyped type | undefined |
void | Only undefined; used only as a return type | – |
| Null | Only null; the type of null, which cannot be written as an annotation | – |
Array | An ordered, growable list of values of any type | null |
Vector.<T> | A dense list whose elements are all of type T | null |
flash.utils.Dictionary | A map whose keys can be objects | null |
XML, XMLList | An XML node, or a list of them | null |
Function | A function or method closure | null |
Class | A class object, such as Sprite itself | null |
Date, RegExp, Error, Namespace, QName | The other core classes; see Top-level API | null |
| Any other class | An instance of that class or a subclass | null |
Assigning null to a Boolean, int,
uint or Number stores that type’s default instead, and
assigning undefined to an Object stores null.
Only * and untyped variables can hold undefined.
Numbers
Three numeric types share the arithmetic operators. int and
uint are exact 32-bit integers, quicker than Number for
counters and indexes; uint suits colours and bit masks. Number
holds integers exactly up to 253 and every fraction to about 15 significant
digits.
| Constant | Value |
|---|---|
int.MIN_VALUE, int.MAX_VALUE | −2147483648, 2147483647 |
uint.MIN_VALUE, uint.MAX_VALUE | 0, 4294967295 |
Number.MAX_VALUE | about 1.79769313486231e+308 |
Number.MIN_VALUE | about 4.940656458412467e-324, the smallest positive value |
Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY | The same as the globals Infinity and -Infinity |
Number.NaN | Not a number, the same as the global NaN; test it with isNaN(), since NaN equals nothing, itself included |
- Division always gives a
Number:7 / 2is3.5even when both values areint. Useint(7 / 2)for3. - Dividing by zero gives
Infinityor-Infinity, and0 / 0givesNaN; it is not an error. - Storing a result in an
intoruintwraps it to 32 bits:int(int.MAX_VALUE + 1)is −2147483648, and auintgiven −3 holds 4294967293. - The bitwise operators work on 32-bit integers and give
int, except>>>, which givesuint. Mathholds the functions and constants:Math.floor,Math.round,Math.sqrt,Math.random,Math.PIand the rest.
String
A String is a sequence of UTF-16 code units; length counts
the units, so a character outside the Basic Multilingual Plane counts as 2. Strings
cannot be changed: every method returns a new string. A String variable can
be null, which is different from the empty string "".
+ joins strings and turns the other operand into a string when one side
is a string. The methods are charAt, charCodeAt,
concat, indexOf, lastIndexOf,
localeCompare, match, replace,
search, slice, split, substr,
substring, toLowerCase, toUpperCase, the
toLocale… versions and the static String.fromCharCode.
Object, *, void, null and undefined
Object– the base of every class. A plainObjectis dynamic, so it doubles as a hash map with string keys:o["name"] = "Ann". It cannot holdundefined.*– the untyped annotation.var x:*andvar xmean the same, except that leaving the annotation out draws a warning in strict mode. The compiler does not check untyped values; the runtime still does.void– a return type for functions that return nothing:function reset():void.null– no object. The default ofString,Objectand every class type.undefined– no value. Read from a missing dynamic property, an untyped variable that was never set, or a missing argument with no default.
null == undefined is true, but null ===
undefined is false.
Array
An Array is a list indexed from 0 whose elements can be of any type and
whose length grows as elements are added. It can be sparse: setting
a[100] on an empty array makes length 101, with the elements
in between undefined. Arrays are dynamic, so named properties can be
added, but those are not elements and do not count in length.
| Method | What it does |
|---|---|
push, pop, unshift, shift | Add or remove at the end or the start. |
splice, slice, concat | Remove and insert in place; copy a range; join arrays into a new one. |
indexOf, lastIndexOf | Find an element using strict equality. |
join, toString | Make a string of the elements. |
reverse, sort, sortOn | Reorder in place; sortOn sorts objects by a named field. |
every, some, filter, forEach, map | Call a function for each element, given as (item, index, array). |
The sort options are bit flags: Array.CASEINSENSITIVE (1),
Array.DESCENDING (2), Array.UNIQUESORT (4),
Array.RETURNINDEXEDARRAY (8) and Array.NUMERIC (16), so
a.sort(Array.NUMERIC | Array.DESCENDING) sorts numbers from the largest.
Vector
Vector.<T> (Flash Player 10 and AIR 1.5 onwards) is an array whose
elements all have the base type T, which the compiler and the runtime
check. It is dense: there are no gaps, and reading or writing beyond the end (other than
adding at index length) is a RangeError. The
fixed property, or the second argument of the constructor, stops the length
from changing. It has most of the methods of Array, but not
sortOn, and its sort is given a compare function.
var names:Vector.<String> = new Vector.<String>();
names.push("Ann", "Bob");
var grid:Vector.<Vector.<int>> = new Vector.<Vector.<int>>(10, true);
var primes:Vector.<int> = new <int>[2, 3, 5, 7];
var fromArray:Vector.<Number> = Vector.<Number>([1.5, 2.5]);
A vector type is fixed to its base type: a Vector.<Sprite> is not a
Vector.<DisplayObject>, though Vector.<*> and
Vector.<Object> accept any element. Vector is the only
class with a type parameter; ActionScript 3.0 has no general generics.
Dictionary
flash.utils.Dictionary is a dynamic object whose keys are compared by
identity, so an object can be a key without being turned into a string. Created with
new Dictionary(true), its keys are weak references: an entry disappears
when nothing else refers to its key object. for … in visits the keys,
for each … in the values, and delete d[key] removes an
entry.
import flash.utils.Dictionary;
var speeds:Dictionary = new Dictionary();
speeds[playerSprite] = 4.5;
speeds[enemySprite] = 2;
for (var key:Object in speeds) {
trace(key, speeds[key]);
}
Other built-in types
| Type | Notes |
|---|---|
XML, XMLList | Native XML; see XML and E4X. typeof gives "xml" for both. |
Function | A function value. Methods taken from an object stay bound to it; see Functions. |
Class | Every class is also an object of type Class: var c:Class = Sprite; var s:Sprite = new c();. Embedded assets are reached through Class variables. |
Date | A moment in time, as milliseconds since 1 January 1970 UTC, with local and UTC fields. |
RegExp | A regular expression; see Regular expressions. |
flash.utils.ByteArray | Raw bytes with methods to read and write integers, floats, strings and AMF objects; big-endian unless endian is changed. |
Namespace, QName | Namespaces and qualified names, for both XML and the language’s own namespaces. |
How values change from one type to another, and how strict mode checks them, is on Type checking and conversion.