User Guide

Classes 121
Instead, a user of the GetSet class will use something that appears to be a property named
publicAccess, but that is really a pair of get and set accessor functions that operate on the
private property named
privateProperty. The following example instantiates the GetSet
class, and then sets the value of the
privateProperty using the public accessor named
publicAccess:
var myGetSet:GetSet = new GetSet();
trace(myGetSet.publicAccess); // null
myGetSet.publicAccess = "hello";
trace(myGetSet.publicAccess); // hello
Getter and setter functions also make it possible to override properties that are inherited from
a superclass, something that is not possible when you use regular class member variables. Class
member variables that are declared using the
var keyword cannot be overridden in a subclass.
Properties that are created using getter and setter functions, however, do not have this
restriction. You can use the
override attribute on getter and setter functions that are
inherited from a superclass.
Bound methods
A bound method, sometimes called a method closure, is simply a method that is extracted
from its instance. Examples of bound methods include methods that are passed as arguments
to a function or returned as values from a function. New in ActionScript 3.0, a bound method
is similar to a function closure in that it retains its lexical environment even when extracted
from its instance. The key difference, however, between a bound method and a function
closure is that the
this reference for a bound method remains linked, or bound, to the
instance that implements the method. In other words, the
this reference in a bound method
always points to the original object that implemented the method. For function closures, the
this reference is generic, which means that it points to whatever object the function is
associated with at the time it is invoked.
Understanding bound methods is important if you use the
this keyword. Recall that the
this keyword provides a reference to a method’s parent object. Most ActionScript
programmers expect that the
this keyword always refers to the object or class that contains
the definition of a method. Without method binding, however, this would not always be true.
In previous versions of ActionScript, for example, the
this reference did not always refer to
the instance that implemented the method. When methods are extracted from an instance in
ActionScript 2.0, not only is the
this reference not bound to the original instance, but also
the member variables and methods of the instance’s class are not available. This is not a
problem in ActionScript 3.0 because bound methods are automatically created when you pass
a method as a parameter. Bound methods ensure that the
this keyword always references the
object or class in which a method is defined.