A Nod to the Past-Prototype-Based Objects

 <  Day Day Up  >  

A Nod to the Past ” Prototype-Based Objects

In ActionScript 1.0, objects in Flash were created using prototype-based language. In the previous version, class-based monikers, such as class and extends , were not part of the language. To define classes and achieve inheritance, a different syntax was required.

A class in ActionScript 1.0 was known as a prototype object. The Language used for prototyping is shown in Listing 5.18. This definition exists in an external file and is included, using the #include directive, on the timeline where it will be used. Notice that class members have no datatypes or access modifiers.

Listing 5.18. Prototype-Based Object Definition
 //Begin prototype definition for ancestor - Loan       //Prototype properties  no datatyping Loan.prototype.principal; Loan.prototype.rate; Loan.prototype.years;       //Constructor function Loan (){ }       //Prototype Method Loan.prototype.setPrincipal = function(thePrincipal){       this.principal = thePrincipal; } //End prototype definition  Loan //Begin prototype definition for descendant - CarLoan       //Prototype properties  no datatypeing CarLoan.prototype.model;       //Constructor function CarLoan (){       super(); }       //Inheritance/prototype chain CarLoan.prototype = Loan; //End prototype definition - CarLoan 

Listing 5.19 shows the comparable, ActionScript 2.0, class-based syntax for the Loan-CarLoan class definitions in Listing 5.18.

Listing 5.19. Class-Based Object Definition
 class Loan{       private var principal:Number ;       private var rate:Number ;       private var years:Number ;       public function Loan(){             trace("in loan constuctor");       }       public function setPrincipal(thePrincipal:Number) {             this.principal = thePrincipal;       } } class CarLoan extends Loan{       private var model:String;       function CarLoan (){             super();       } } 

Another major difference in ActionScript 2.0 is the way visual objects in a Library are linked to a class definition. In ActionScript 1.0, a prototype object definition can be linked to a Library item by giving explicit instruction to the compiler. The Library item relationship is established by explicitly including the file on the first frame of the symbols timeline. This guarantees that the code is available to be compiled as part of the definition. In Flash, when a SWF is compiled, it contains anything on the timeline and only Library symbols that have been identified as required in the final SWF. The symbol must be marked for import as part of the Library symbol properties. The symbol linkage properties are set to export the symbol in the first frame. Figure 5.10 is the Linkage panel for Library symbols. It is accessed through the context menu on the Library item or in the Main Library menu at the top of the Library panel.

Figure 5.10. Library Symbol Linkage panel.


In Figure 5.10, the Identifier is the unique name associated with the symbol in the Library. In ActionScript 1.0, this was the key to associating an external prototype definition with a Library symbol. The unique identifier was used to create a relationship between the external definition and the symbol using the syntax shown in Listing 5.20.

Listing 5.20. Prototype Object Linkage
 // Linkage (associating a prototype definition  with a visual object) Object.registerClass("LoanCalcSymbol", LoanCalc); 

This works assuming the prototype object is named LoanCalc and that the file in which it is defined has been included in the first frame of the timeline for the Library symbol.

In ActionScript 2.0 the linkage is much simpler. In Figure 5.10, the AS 2.0 property links the symbol to an external class file. There is no need to explicitly include the file. The external file is located by the compiler through the classpath. An advantage of this technique is that more that one symbol can use the same external class definition. In ActionScript 1.0, that is not possible. Library symbols and class definition have a one-to-one relationship.

 <  Day Day Up  >  

Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162

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