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 (much like 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 will be the originally referenced object, not the
prototype object.
If invoked incorrectly,
describes errors that may occur:
Error condition
is not a valid property name; for instance, an
prop
empty string.
is not a valid function object.
getFunc
is not a valid function object.
setFunc
Example
Usage 1: An object has two internal methods,
, can be used to invoke these methods when it is either set or retrieved. A third internal
bookcount
method,
getTitle()
function Book() {
this.setQuantity = function(numBooks) {
this.books = numBooks;
}
this.getQuantity = function() {
return this.books;
}
this.getTitle = function() {
return "Catcher in the Rye";
}
this.addProperty("bookcount", this.getQuantity, this.setQuantity);
this.addProperty("bookname", this.getTitle, null);
}
myBook = new Book();
myBook.bookcount = 5;
order = "You ordered " + myBook.bookcount + " copies of " + myBook.bookname;
When a script retrieves the value of
automatically invokes
myBook.bookcount
does not specify a set function, so attempts to modify
576
Chapter 12: ActionScript Dictionary
Object.addProperty()
, returns a read-only value that is associated with the property
myBook.bookcount
myBook.getQuantity()
, the interpreter invokes
may fail with an error. The following table
What happens
Returns
and the property is not added.
false
Returns
and the property is not added.
false
Returns
and the property is not added.
false
and
setQuantity()
, the ActionScript interpreter
. When a script modifies the value of
myObject.setQuantity()
are ignored.
bookname
. A property,
getQuantity()
bookname
. The
property
bookname
:
Need help?
Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?
Questions and answers