Add methods to native Classes in ActionScript 3.0 – Prototype functions

Hi again!
Yesterday I’ve found out a cool new feature of ActionScript 3.0. You will sometimes have to use associative Arrays. The problem with these is that you can’t easily loop through them with a for-loop like

  1. var    myAssocArray:Array = [],
  2.         i            :int,
  3.         intLength    :int;
  4.  
  5. myAssocArray['foo'] = 'one';
  6. myAssocArray[1] = 'two';
  7. myAssocArray['bar'] = 'three';
  8. myAssocArray[0] = 'four';
  9. intLength = myAssocArray.length;
  10.  
  11. for(; i< intLength; ++i)
  12. {
  13.     trace(myAssocArray[i]);
  14. }

The trace will probably print out “four, two, undefined, undefined”. The fact is that you can get the elements 0 & 1 via the variable i, but the the other keys can’t be accessed with this method.
As I also like PHP very much, I also know the function array_keys(array) of PHP, which returns the key-values of the array given in the parameters. I thought that I really need this functionality within AS3, so I tried a lot, searched the internet and finally got it! It’s pretty simple for those knowing AS2 – prototype functions.
What do we need to do? Place the following code in your main, unnamed package below or above the class-definition and you can use it everywhere else in the code:

  1. package
  2. {
  3.     public class foo
  4.     {
  5.         public function foo():void
  6.         {
  7.             trace('bar');
  8.         }
  9.     }
  10.    
  11.     Array.prototype.arrayKeys = function():Array
  12.     {
  13.         var    arrReturn    :Array = [],
  14.             strKey        :String;
  15.  
  16.         for(strKey in this)
  17.         {
  18.             if(typeof this[strKey] == 'function') continue;
  19.             trace('in Array:: value->'+ strKey +' typeOf:'+ typeof strKey +' proto:'+ ((this[strKey] !== false) ? typeof this[strKey] : 'NULL'));
  20.             arrReturn[arrReturn.length] = strKey;
  21.         }
  22.  
  23.         return arrReturn;
  24.     }
  25.    
  26.     Number.prototype.give = function():String
  27.     {
  28.         return 'Number::giveItToMeBaby';
  29.     }
  30. }

You can now use this method to get the keys of the variable:

  1. var arrKeys        :Array = myAssocArray.arrayKeys(),
  2.     i            :int,
  3.     intLength    :int = arrKeys.length;
  4.  
  5. for(; i < intLength; ++i)
  6. {
  7.     trace(myAssocArray[arrKeys[i]]);
  8. }

Pretty easy, isn't it? Well, it is easy because the native Array-Class is dynamic, but what do we do with Number (final class)? Let's see...

  1. var num:Number = 4;
  2. num.give(); //doesn't work! final class, can't add methods to it?
  3.  
  4. num['give'](); //does work :) you can dynamically add methods to a class even if it's final; problem is that this code is hard to read, but it actually works

So far...
Daniel

1 Comment yet »

  1. Daniel Bunte wrotes

    am 12. August 2009 @ 16:14

    Please note:

    The problem with Array-Prototype-Methods is that if you run through that array with
    for(var strKey:* in arrObjects)
    {
    trace(strKey);
    }

    you will probably also get the function as a methdod.
    So the trace will will put “function() Function” or something similar on the screen.

Comment RSS · TrackBack URI

Leave a comment

Name: (required)

E-Mail: (required)

Website:

Comment: