A class is the whole of an ActionScript 2.0 class file. The syntax is new, but what it makes is the prototype-based object of ActionScript 1.0: the class becomes a constructor function, its methods go on the constructor’s prototype, and its rules are enforced only by the compiler.
The declaration
class-decl = ["dynamic"] ["intrinsic"] "class" dotted-name
["extends" dotted-name] ["implements" dotted-name { "," dotted-name }]
"{" { member } "}"
member = ["public" | "private"] ["static"] "var" name [":" type] ["=" expr] ";"
| ["public" | "private"] ["static"] "function" ["get" | "set"] name
"(" [param { "," param }] ")" [":" type] block
- The name is the full dotted name, and must match the file’s path from a classpath
folder:
class com.example.Shapeincom/example/Shape.as. - A class extends at most one class, and implements any number of interfaces.
Without
extendsit extendsObject. - Classes cannot be nested, and there is one per file.
- Members may come in any order. Their modifiers go before
varorfunction.
Instance variables
A var in the class body declares a property that each object has. Its
type is checked wherever the compiler sees it used.
A starting value given in the declaration is not set on each new object: the
compiler stores it on the class’s prototype, where every object finds it until it sets
its own. For numbers, strings and booleans this makes no difference. For an array or
object it does: var items:Array = []; gives every instance the same
array. Such values are set in the constructor instead.
class com.example.Basket {
private var total:Number = 0; // fine: each object sets its own when it changes
private var shared:Array = []; // one array for every Basket
private var items:Array; // set in the constructor instead
public function Basket() {
items = [];
}
}
Constructors and super
- The constructor is the method named after the class. It has no return type, and there is at most one. A class without one gets an empty constructor.
super(arguments)in a constructor runs the superclass’s constructor on the new object;super.method(arguments)in any method runs the superclass’s version of an overridden method.- A class made for a movie clip symbol is constructed by the player when the clip is
created, with no arguments; values for it are passed as the
attachMovieinit object, which is applied before the constructor runs.
Methods
A function in the class body is a method, shared by all instances
through the prototype. Inside it, members of the class need no this..
A subclass overrides a method by declaring one of the same name; there is no
override keyword and no overloading. Methods are hidden from
for..in.
public and private
| Modifier | Means |
|---|---|
public | Usable from anywhere. Members are public when no modifier is given. |
private | Usable only inside the class. In practice the compiler also lets subclasses use them, so it works like protected in other languages. |
Both are checked only when compiling, and only for code that uses a typed reference.
When the movie runs, every member is an ordinary property that any code can read with
an untyped reference or square brackets. There is no protected or
internal.
static
static members belong to the class, not to its objects, and are used as
ClassName.member, or by their bare name inside the class. A static
variable’s starting value is set once, when the class is defined, which happens before
the frame scripts of the frame the classes are exported in. Static methods have no this for an instance and
cannot use instance members.
class com.example.IdMaker {
private static var next:Number = 1;
public static function make(prefix:String):String {
return prefix + (next++);
}
}
// elsewhere: IdMaker.make("clip") gives "clip1", then "clip2"
Getters and setters
A method written with get or set before its name is used as
if it were a property:
class com.example.Volume {
private var level:Number = 50;
public function get percent():Number {
return level;
}
public function set percent(value:Number):Void {
level = Math.max(0, Math.min(100, value));
}
}
// v.percent = 120; trace(v.percent); gives 100
A getter takes no parameters; a setter takes one. The compiler turns them into
methods named __get__percent and __set__percent and joins
them with addProperty, so they also work for untyped code at run time. A
property with only a getter is given an empty setter, so writing to it does nothing.
get and set are not allowed in interfaces.
dynamic
Normally the compiler reports any property used on a typed object that its class does
not declare. A class marked dynamic lets code add and read undeclared
properties without errors, as with Object. Adobe’s reference says that
subclasses of dynamic classes are dynamic too. Classes for movie clip symbols declare
the instances inside the symbol as variables, such as
var label_txt:TextField;, so that the compiler knows them and their
types.
intrinsic
An intrinsic class describes a class that exists already, so that the
compiler can check code that uses it, but compiles to nothing. Its methods have no
bodies. Flash declares all the built-in classes this way in its configuration folder,
and developers wrote intrinsic files for classes defined elsewhere, such as in
ActionScript 1.0 code or in a movie loaded later.
intrinsic class com.example.LegacyMenu {
var items:Array;
function open(index:Number):Void;
function close():Void;
}
What a class is when the movie runs
- The class is a constructor function stored on
_globalunder its package path:com.example.Volumeis_global.com.example.Volume. - Methods, getters, setters and starting values are properties of its
prototype, hidden fromfor..in. Static members are properties of the function. extendslinks the prototypes, as__proto__does in ActionScript 1.0;instanceofworks as usual.- Types,
privateandstaticleave no trace beyond where things are stored.
The code the compiler writes for this is shown on Compiler.