Object-Oriented Theory vs. ActionScript Reality

The implementation of classic object orientation within Flash is far less rigorous than in many other languages, like Java or C++.

ActionScript does not provide the security of meaningful encapsulation. We have seen that an object's private functions and data are not protected from other ActionScript entities. In fact, outside code not only can examine and alter any variable in an object, it also can create new variables and install new functions. Any programmer can easily overwrite the native functions of another programmer's object ”at run time. Not only can the functions of an individual instance be overwritten, but they can also be overwritten in the actual class definition. In fact, a programmer can change the class definition of someone else's objects, and those objects that are already in place and working will immediately exhibit the new behavior. This is rather dangerous.

Other features of object-oriented languages are available in Flash. It is possible to create objects. With a bit of work you can create objects that exhibit inheritance. The actual syntax for creating such hierarchical objects bears attention, as it is not documented and it is far from clear. ActionScript objects are never explicitly declared. Rather, they are implied with the following elements:

A constructor is a function that initializes new instances of an object. Every ActionScript function has the potential to be the constructor of objects with the same name . Simply calling the function with the keyword new identifies it as an object constructor. A function named Disk will construct Disk objects when used in the statement.

 floppy = new Disk(); 

Within an object constructor function, the keyword this points to the new object being created. It is commonly used to initiate variables within the new object.

 this.formatted = false; 

It can also be used to include methods in the object:

 this.format = function ( ) ( ... 

However, this places an individual copy of the function in each new instance. There is a more efficient way to construct objects. These properties (functions and variables) can be added to the object's prototype. The prototype is a property that all functions have. It is used by objects to identify the functions and variables of which it is composed . The statement

 Disk.prototype.format = function ( ) ( ... 

establishes a format() function that all Disk objects will automatically have. It is interesting that the function can be installed into the prototype at any time, even after the constructor has been used to make new instances of the object. All existing instances of the object will suddenly have the new function.

If an object instance does not have a function that is being called, the ActionScript engine seeks it in the prototype. It locates the prototype of any object instance by looking at the object's _proto_ variable, which points to the class prototype.

We can use this trick to create a hierarchical object structure, so that we can gain object inheritance.

 FloppyDisk.prototype._proto_ = Disk.prototype; 

This sets up the Disk object as a parent of the FloppyDisk object. Here is how we would create the hierarchy of Figure 2.1 in ActionScript code.

Figure 2.1. Adding Dynamic Text to the Button


 // Defining the Disk Object function Disk ( ) {       trace ( "new Disk" ); } Disk.prototype.label = "unlabelled disk "; Disk.prototype.directory = function ( ) {       trace ( "Directory of Disk... .") ; } Disk.prototype.format = function () {       trace ( "Disk formatted."); } // Defining the FloppyDisk Object function FloppyDisk ( ) {       trace ( "new FloppyDisk" ) ; } FloppyDisk.prototype._proto_ =. Disk.prototype; FloppyDisk.prototype.directory = function ( ) {       trace ( "Directory of Disk... .") ; } FloppyDisk.prototype.format = function () {       trace ( "Special floppy format tweak");       this._proto_._proto_.format (); FloppyDisk.prototype.eject = function () {       trace ( "Eject floppy disk"); } // Here we use the function fd = new FloppyDisk();  // constructor fd.directory();         // inherited function fd.format();            // extended function fd.eject();             // function trace ( "Label reads:" + fd.label); // class variable stop(); 

The resulting output is

 new FloppyDisk Directory of Disk... . Special floppy format tweak Disk formatted. Eject floppy disk Label reads: unlabelled disk 

We see that fd was able to find eject() in its own prototype. fd found the variable label and the function directory() in its parent's prototype. The format() function used code from the prototypes of both FloppyDisk and Disk.

Flash and XML[c] A Developer[ap]s Guide
Flash and XML[c] A Developer[ap]s Guide
ISBN: 201729202
Year: 2005
Pages: 160

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net