
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
68
|
Chapter 4: Classes
To fix the problem, you could add the line shown in bold to the Box class definition:
class Box {
var width:Number;
var height:Number;
var description:String;
}
In ActionScript 1.0, a reference to a nonexistent property did not gen-
erate an error. The interpreter simply created a new property of the
specified name the first time it was encountered at runtime.
Even if our Box class defines a property by the name description, it’s often consid-
ered bad form to access properties directly from outside a class, as does the code:
var b:Box = new Box( );
b.description = "A square object";
When a property is accessible to code outside a class, code that uses the property
becomes dependent on the class definition, limiting its flexibility. If, for example, we
were to rename the
description property to desc, we’d also have to change all code
outside the class that referred to
description. If enough external code relies on a
class’s property name, changing the name can become unfeasible. Furthermore,
when a property is accessible outside a class, class-external code might unwittingly
assign illogical or illegal values to the property. For example, code external to the
Box class might assign a negative or fractional value to the
height property, which
may not be considered legal ...