378
Chapter 6: ActionScript Core Classes
You can add getter/setter properties to prototype objects. If you add a getter/setter property to a
prototype object, all object instances that inherit the prototype object inherit the getter/setter
property. This makes it possible to add a getter/setter property in one location, the prototype
object, and have it propagate to all instances of a class (similar to adding methods to prototype
objects). If a get/set function is invoked for a getter/setter property in an inherited prototype
object, the reference passed to the get/set function is the originally referenced object—not the
prototype object.
If invoked incorrectly,
Object.addProperty()
can fail with an error. The following table
describes errors that can occur:
Example
In the following example, an object has two internal methods,
setQuantity()
and
getQuantity()
. A property,
bookcount
, can be used to invoke these methods when it is either
set or retrieved. A third internal method,
getTitle()
, returns a read-only value that is associated
with the property
bookname
. When a script retrieves the value of
myBook.bookcount
, the
ActionScript interpreter automatically invokes
myBook.getQuantity()
. When a script modifies
the value of
myBook.bookcount
, the interpreter invokes
myObject.setQuantity()
. The
bookname
property does not specify a
set
function, so attempts to modify
bookname
are ignored.
function Book() {
this.setQuantity = function(numBooks:Number):Void {
this.books = numBooks;
};
this.getQuantity = function():Number {
return this.books;
};
this.getTitle = function():String {
return "Catcher in the Rye";
};
this.addProperty("bookcount", this.getQuantity, this.setQuantity);
this.addProperty("bookname", this.getTitle, null);
}
var myBook = new Book();
myBook.bookcount = 5;
trace("You ordered "+myBook.bo" copies of "+myBook.bookname);
// output: You ordered 5 copies of Catcher in the Rye
Error condition
What happens
prop
is not a valid property name; for example, an
empty string.
Returns
false
and the property is not added.
getFunc
is not a valid function object.
Returns
false
and the property is not added.
setFunc
is not a valid function object.
Returns
false
and the property is not added.
Summary of Contents for FLEX-FLEX ACTIONSCRIPT LANGUAGE
Page 1: ...Flex ActionScript Language Reference...
Page 8: ......
Page 66: ...66 Chapter 2 Creating Custom Classes with ActionScript 2 0...
Page 76: ......
Page 133: ...break 133 See also for for in do while while switch case continue throw try catch finally...
Page 135: ...case 135 See also break default strict equality switch...
Page 146: ...146 Chapter 5 ActionScript Core Language Elements See also break continue while...
Page 808: ...808 Chapter 7 ActionScript for Flash...
Page 810: ...810 Appendix A Deprecated Flash 4 operators...
Page 815: ...Other keys 815 Num Lock 144 186 187 _ 189 191 192 219 220 221 222 Key Key code...
Page 816: ...816 Appendix B Keyboard Keys and Key Code Values...
Page 822: ...822 Index...