
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
94
|
Chapter 4: Classes
ActionScript 1.0 did not have a formal means of defining class meth-
ods. By convention, class methods were defined on a class’s construc-
tor function, as follows:
// Class constructor
function Box ( ) {
}
// Class method
Box.findLarger = function (box1, box2) {
if (box2.getArea() > box1.getArea( )) {
return box2;
} else {
return box1;
}
};
ActionScript 1.0 class methods could be accessed via the class con-
structor only, as in
Box.findLarger( ) (not findLarger( )). Further-
more, in an ActionScript 1.0 class method, the
this keyword was
legal; it referred to the class constructor function.
// Class constructor
function Box ( ) {
}
// Class method
Box.showThis = function ( ) {
trace(this == Box); // Displays: true
};
Accessor Methods
Earlier we learned that it’s good OOP practice to declare a class’s instance properties
as private, meaning that they cannot be read or modified by code outside of the class.
However, most objects must expose some means of examining and changing their
state (i.e., provide a publicly accessible means of retrieving and assigning the object’s
properties). To allow external code to read and modify private properties, we create
so-called accessor methods, which come in two varieties:
• Those that retrieve a property’s value
• Those that set a property’s ...