4 Replies Latest reply: Apr 29, 2012 11:34 AM by birnerseff RSS

    How to extend system classes

    birnerseff Community Member

      Hi,

       

      just an example: I am using lots of arrays in my code, and so repeat the stanza

      somearray[somearray.length-1]

      all over the place.

      Now, I could do something like

       

      public dynamic class MyArray extends Array

      {    public function get last():*

           {    return this[this.length-1];

           }

      }

       

      and replace all instances of new Array() by new MyArray().

      This would allow to use

      somearray.last

       

      However, this would lose the shortcut [], and the "last" accessor would not be available on arrays set up by system code (say as part of data

      collections)

      There are similar examples for static functions that I would like to add to some classes. I could, of course, collect them into a class MathUtil or 'GeomUtil,

      but I would prefer to relate them to, say Point or Vector3D

       

      I have seen "prototype chain" etc mentioned, would that help?

        • 1. Re: How to extend system classes
          kglad CommunityMVP

          create a static method:

           

          package {

             

              public class A {

           

                  public static function last(a:Array):*{

                      return a[a.length-1];

                  }

              }

          }

          • 2. Re: How to extend system classes
            birnerseff Community Member

            Hi,

             

            well that would give me

             

            A.last(myarray)

             

            instead of the desired

             

            myarray.last

             

            Probably even that makes code somewhat more readable

            • 3. Re: How to extend system classes
              dmeN Community Member

              >>However, this would lose the shortcut [], and the "last" accessor would not be available on arrays set up by system code (say as part of data collections)

               

              I would say not an issue to both of those. When do you really use []? I never use it... and who cares about arrays set up by system code? This solves your issue... but while I don't see your first solution as a problem, I would use kglads way - and do actually. I have an array utilities class and make use of it exactly like that: A.last(myarray) and I do think it makes the code more readable as well. Just my .02

              • 4. Re: How to extend system classes
                birnerseff Community Member

                Hi,

                 

                well, I really prefer [] over new Array() and {} over new Object() ....

                 

                I have one class that is declared

                 

                public dynamic class something extends Array

                 

                For the moment I have added

                 

                public function get lastsomething():*

                 

                to it. I found that telling plain arrays and this special construct apart (in code just a few weeks old) was not that easy, so I will probably use distinctive member names more often