Object
553
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
The previous example works, but the properties
bookcount
and
bookname
are added to every
instance of the
Book
object, which requires having two properties for every instance of the
object. If there are many properties, such as
bookcount
and
bookname,
in a class, they could
consume a great deal of memory. Instead, you can add the properties to
Book.prototype
so
that the
bookcount
and
bookname
properties exist only in one place. The effect, however, is
the same as that of the code in the example that added
bookcount
and
bookname
directly to
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
Book.prototype
are encountered. The following example shows how to add the properties to
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.bo" copies of "+myBook.bookname);
Содержание Flash Lite 2
Страница 1: ...Flash Lite 2 x ActionScript Language Reference...
Страница 22: ...22 Contents...
Страница 244: ...244 ActionScript language elements...
Страница 760: ...760 ActionScript classes...