< 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"):
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.
|
< Day Day Up > |