The previous example works, but the properties
instance of the
Book
object. If there are many properties, such as
consume a great deal of memory. Instead, you can add the properties to
that the
and
bookcount
the same as that of the code in the example that added
every instance. If 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
are encountered. The following example shows how to add the properties to
Book.prototype
:
Book.prototype
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. Rather than defining the
define the
class in an external file named Book.as. The following code must be in a
Book
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";
}
}
994
ActionScript classes
object, which requires having two properties for every instance of the
properties exist only in one place. The effect, however, is
bookname
and
bookcount
and
bookcount
bookname,
bookcount
function and editing
Book
are added to every
bookname
in a class, they could
Book.prototype
and
directly to
bookname
Book.prototype
so
, you
Need help?
Do you have a question about the FLASH 8-ACTIONSCRIPT 2.0 LANGUAGE and is the answer not in the manual?