
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
116
|
Chapter 4: Classes
To allow the width and height properties to be customized for each Box instance, we
add parameters to our constructor function definition:
public function Box (w:Number, h:Number) {
// Initialize width and height, using the
// values passed to the parameters w and h.
width = w;
height = h;
}
A constructor function’s parameter values are passed to it via the new operator at
object-creation time, as follows:
new SomeClass(value1, value2,...valuen);
where SomeClass is the name of the class being instantiated and value1, value2, ...
valuen are the values passed to the constructor function. For example, to create a
new Box instance with an initial
width of 2 and height of 3, we’d use:
new Box(2, 3);
Supplying parameter values to a constructor function is the ActionScript equivalent
of genetically predetermining that your baby should be a girl, weigh 7 pounds, and
have brown hair.
Constructor functions normally use parameter values to set property values, but
parameters can also more generally govern what should happen when an instance is
created. For example, the constructor function for a Chat class might include a
parameter,
doConnect, that indicates whether the Chat instance should automatically
connect to the chat server upon creation:
class Chat {
public function Chat ...