The previous example works, but the properties
instance of the
object, which requires having two properties for every instance of the object.
Book
If there are many properties, such as
great deal of memory. Instead, you can add the properties to
and
bookcount
bookname
that of the code in the example that added
an attempt is made to access either property in a Book instance, the property's absence will cause
the prototype chain to be ascended until the versions defined in
encountered. The following example shows how to add the properties to
function Book() {}
Book.prototype.setQuantity = function(numBooks:Number):Void {
this.books = numBooks;
};
Book.prototype.getQuantity = function():Number {
return this.books;
};
Book.prototype.getTitle = function():String {
return "Catcher in the Rye";
};
Book.prototype.addProperty("bookcount", Book.prototype.getQuantity,
Book.prototype.setQuantity);
Book.prototype.addProperty("bookname", Book.prototype.getTitle, null);
var myBook = new Book();
myBook.bookcount = 5;
trace("You ordered "+myBook.bookcount+" copies of "+myBook.bookname);
The following example shows how to use the implicit getter and setter functions available in
ActionScript 2.0. For more information, see
than defining the
Book
external file named Book.as. For more information, see
The following code must be in a separate external file named Book.as that contains only this class
definition and resides within the Flash application's classpath:
class Book {
var books:Number;
function set bookcount(numBooks:Number):Void {
this.books = numBooks;
}
function get bookcount():Number {
return this.books;
}
function get bookname():String {
return "Catcher in the Rye";
}
}
The following code can then be placed in a FLA file and will function the same way as it does in
the previous examples:
var myBook:Book = new Book();
myBook.bookcount = 5;
trace("You ordered "+myBook.bookcount+" copies of "+myBook.bookname);
bookcount
properties exist only in one place. The effect, however, is the same as
bookcount
"Implicit getter/setter methods" on page
function and editing
Book.prototype
and
bookcount
bookname
and
in a class, they could consume a
bookname,
Book.prototype
and
directly to every instance. If
bookname
Book.prototype
, you define the
"Creating and using classes" on page
are added to every
so that the
are
:
Book.prototype
63. Rather
class in an
Book
Object.addProperty()
51.
379
Need help?
Do you have a question about the FLEX-FLEX ACTIONSCRIPT LANGUAGE and is the answer not in the manual?