6.7 Augmenting Built-in Classes and Objects

 <  Day Day Up  >  

In the previous section we learned how to subclass a built-in class. New properties and methods can also be added directly to an existing built-in class, without subclassing it. However, the technique uses an old-school ActionScript 1.0 hack, is heavy-handed, and requires that we break the important OOP precept that warns us to keep method and property definitions contained within a class definition. Nevertheless, it does work and can be convenient if used sparingly and prudently.

Here's the general syntax for adding a new method to a built-in class at runtime:

   ClassName   .prototype.   methodName   = function (   param1   ,   param2   , ...   paramn   ) {   statements   }; 

Here's the general syntax for adding a new property to a built-in class at runtime:

   ClassName   .prototype.   propertyName   =   value   ; 

For example, the following code adds a new method, isEmpty( ) , to the built-in String class. The isEmpty( ) method returns true when a string has no characters in it; otherwise , false :

 String.prototype.isEmpty = function ( ) {   return (this == "") ? true : false; }; // Usage example: var s1:String = "Hello World"; var s2:String = ""; trace(s1.isEmpty( ));  // Displays: false trace(s2.isEmpty( ));  // Displays: true 

However, the previous code example ”and this entire technique ”has a problem: the newly defined method or property isn't added until runtime; therefore, the compiler has no idea that the new member exists and will generate an error if it is used with typed data. For example, the code in the preceding example creates two typed variables , s1 and s2 , so the code yields this error:

 There is no method with the name 'isEmpty'. 

To avoid this problem, we're forced into a nasty, hacked solution. We inform the compiler that we've defined a String.isEmpty( ) method by adding it to the String class's intrinsic class definition, found in the location listed next . In each of the following file paths, substitute your operating-system user -account name for USER and your Flash language code for LANGUAGE CODE (the LANGUAGE CODE for English is "en"):


Built-in String intrinsic class definition on Windows (this folder is a hidden folder on some computers) :

c:\Documents and Settings\USER\Local Settings\Application Data\Macromedia\Flash MX 2004\LANGUAGE CODE\Configuration\Classes\String.as


Built-in String intrinsic class definition on Mac :

HD:/Users/USER/Library/Application Support/Macromedia/Flash MX 2004/LANGUAGE CODE/Configuration/Classes/String.as

Here's an excerpt from a modified version of the String.as intrinsic class definition. It shows only the new method and doesn't show the rest of the intrinsic class definition:

 //*********************************************************************** // ActionScript Standard Library // String object //*********************************************************************** intrinsic class String {   // We add this line to the file so that the compiler   // knows about our new   isEmpty( )   method   function isEmpty( ):Boolean; } 

The String.as fix itself has a problem: it must be implemented on every computer that uses the String.isEmpty( ) method. That is, if you fix the String.as file on your work computer and then take your work home, you'll have to fix it on your home computer too. This makes the source code distribution for any application that uses String.isEmpty( ) very awkward , particularly if more than one application on your system requires changes to the String.as file! Hence, as mentioned earlier, you should use this technique only when absolutely necessary, and you should be particularly hesitant to use it if you plan on distributing your code among a team or to the public. In fact, if you're convinced you regularly need to add new methods and properties to built-in classes, you may simply want to use ActionScript 1.0 or perhaps not declare datatypes in your ActionScript 2.0 code. In ActionScript 2.0, you should avoid the practice unless you are working with old code.

The compiler does not generate errors when new methods or properties are accessed on the following built-in classes: Array , ContextMenu , ContextMenuItem , Function , FunctionArguments (a.k.a. the Arguments object), LoadVars , MovieClip , Object , and TextField . Those classes are all defined as dynamic , which allows new methods and properties to be added to their instances without errors.


 <  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