
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Methods
|
105
callback function wouldn’t have had access to the Box object, and the width and
height properties could not have been set.
Note that the
dimensions_xml object is kept alive until its load( ) operation completes
and onLoad( ) fires. Once onLoad( ) has executed, if no other references to the
dimensions_xml object exists (as in the case is in our example), the interpreter auto-
matically marks the object for deletion, preventing memory waste.
Getter and Setter Methods
Earlier we learned about accessor methods, which are public methods that assign
and retrieve the value of a private property. Some developers consider accessor meth-
ods cumbersome. They argue that:
b.setHeight(4);
is more awkward than:
b.height = 4;
However, in our earlier study, we saw that direct property assignments such as b.
height = 4
aren’t ideal OOP practice and can lead to invalid property assignments.
To bridge the gap between the convenience of property assignment and the safety of
accessor methods, ActionScript 2.0 supports “getter” and “setter” methods. Getter
and setter methods are accessor-like methods, defined within a class body, that are
invoked automatically when a developer tries to get or set a property directly.
To define a getter method, we use the following general syntax:
function get propertyName ...