MACROMEDIA FLASH 8-FLASH LITE 2.X ACTIONSCRIPT LANGUAGE Reference page 553

Flash lite 2.x actionscript language reference
Hide thumbs Also See for FLASH 8-FLASH LITE 2.X ACTIONSCRIPT LANGUAGE:
Table of Contents

Advertisement

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
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);
object, which requires having two properties for every instance of the
properties exist only in one place. The effect, however, is
bookname
and
bookcount
bookname
and
bookcount
bookname,
bookcount
are added to every
in a class, they could
Book.prototype
and
directly to
bookname
Object
so
553

Advertisement

Table of Contents
loading

Table of Contents