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,
describes errors that can occur:
Error condition
is not a valid property name; for example, an
prop
empty string.
is not a valid function object.
getFunc
is not a valid function object.
setFunc
Example
In the following example, an object has two internal methods,
getQuantity()
set or retrieved. A third internal method,
with the property
ActionScript interpreter automatically invokes
the value of
myBook.bookcount
property does not specify a
bookname
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.bookcount+" copies of "+myBook.bookname);
// output: You ordered 5 copies of Catcher in the Rye
378
Chapter 6: ActionScript Core Classes
Object.addProperty()
. A property,
bookcount
. When a script retrieves the value of
bookname
, the interpreter invokes
can fail with an error. The following table
What happens
Returns
false
Returns
false
Returns
false
, can be used to invoke these methods when it is either
, returns a read-only value that is associated
getTitle()
myBook.getQuantity()
myObject.setQuantity()
function, so attempts to modify
set
and the property is not added.
and the property is not added.
and the property is not added.
and
setQuantity()
myBook.bookcount
. When a script modifies
. The
bookname
, the
are ignored.
Need help?
Do you have a question about the FLEX-FLEX ACTIONSCRIPT LANGUAGE and is the answer not in the manual?