3.4 Built-in Dynamic Classes

 <  Day Day Up  >  

We saw earlier that trying to dynamically add a property to the Date class generates a compile-time error in ActionScript 2.0. To allow new properties and methods to be added to a class's instances without generating a compile-time error, we can use a dynamic class . You can define your own dynamic classes, but some built-in classes are dynamic by default. Due partially to the architecture of the Flash Player and partially to the heritage of ActionScript 1.0, the following native ActionScript 2.0 classes are dynamic:

  • Array

  • ContextMenu

  • ContextMenuItem

  • Function

  • FunctionArguments (a.k.a. the Arguments object)

  • LoadVars

  • MovieClip

  • Object

  • TextField

When you attempt to access a nonexistent property or method on an object of one of the preceding dynamic classes, the ActionScript 2.0 compiler does not generate an error. For example, the following code yields no error:

 var dataSender:LoadVars = new LoadVars( ); dataSender.firstName = "Rebecca";  // No error, even though                                     // the   LoadVars   class doesn't                                     // define the   firstName   property. 

However, type mismatch errors may still occur when using the preceding dynamic classes. For example, the following code creates a variable of type Array and attempts to place a Date instance into it:

 var list:Array = new Date( ); 

It yields the following error:

 Type mismatch in assignment statement: found Date where Array is required. 

figs/as1note.gif As we emphasized earlier, in ActionScript 1.0, unlike in ActionScript 2.0, it was possible to add a new property or method to any object at runtime. Furthermore, ActionScript 1.0 allowed you to add new methods and properties to an entire class at runtime (via the class's prototype object). ActionScript 2.0 considers adding new properties and methods to classes or objects at runtime bad form. We'll cover this limitation, its motivation, and its workarounds in Chapter 6, under "Augmenting Built-in Classes and Objects."

 <  Day Day Up  >  


Essential ActionScript 2.0
Essential ActionScript 2.0
ISBN: 0596006527
EAN: 2147483647
Year: 2004
Pages: 177
Authors: Colin Moock

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