11 Replies Latest reply on Feb 9, 2007 8:30 AM by Peter Lorent

    Extending Array class

    Pop_Clingwrap Level 1
      I want to define a new method to search arrays for specific values.
      I have a new class defined like this:

      class Array2 extends Array{
      function Array2(){

      function myMethod(){

      In my code I use this line to declare a new instance:
      var myArray:Array2=new Array2(1,2,3,4,5);

      If I now use use:


      it returns 'undefined'. What is the reason for this and is there a way round it?
      If I define the myArray without any values and then use the push() method it seems to work but I need Array2 to work exactly the same as Array only with some additional methods.

      Cheers for any and all help
        • 1. Extending Array class
          Jan-Paul K.
          you are calling an empty constructor, so all porvided values simply get lost.
          You either need to fill your constructor with some code that properly uses the given parameters or simply delegate the constructor call to the superclass's constructor. Doing nothing in the constructor function will create an empty instance of the Array2 class no matter what parameter you provide
          • 2. Re: Extending Array class
            Peter Lorent Level 2
            You can't extend the Array Class in the way you want. If you pass an array in the constructor your instance will become an object. You would need the prototype definition.
            • 3. Re: Extending Array class
              Jan-Paul K. Level 1
              if you don't need the constructor to work you can use this code, but you have to use the array.push method or index access to fill the array. I''m still trying to get the constructor to work, but I am not quite sure how the constructor of the standard Array.as class works:

              class Array2 extends Array {
              function Array2(value:Object){

              function myMethod(){
              trace("Hello world");
              • 4. Re: Extending Array class
                Pop_Clingwrap Level 1
                I've been messing around with it all morning but cant get it to work either. The array.push method will do the job but if you get the constructor working i would be really interested to see.
                I am fairly new to writing classes and need all the info on them I can find.

                Cheers again for all your help :)
                • 5. Re: Extending Array class
                  Level 7
                  Try this:

                  class Array2 extends Array
                  function Array2(){
                  for (var i=0; i< arguments.length; i++){
                  this = arguments;

                  • 6. Re: Extending Array class
                    Jan-Paul K. Level 1
                    here is a dirty hack for the constructor that gives you at least the possibility to create an Arra2 instance and fill it with values in one step:

                    function Array2(array:Array){
                    for(var i = 0 ; i < array.length ; i++)
                    this.push(array );

                    obviously you will have to provide an array as the constructors parameter, so this is not a 100% solution to your requrest, but at least you don't have to care about pushing all those values in

                    var myArray:Array2=new Array2(new Array(1,2,3,4,5));

                    should do the job
                    • 7. Extending Array class
                      Jan-Paul K. Level 1
                      cool tip Raymond Basque, I didn't know about this arguments keyword.
                      But shouldn't it be like
                      • 8. Re: Extending Array class
                        Level 7
                        Yup -- push would be the correct way to do it -- much faster than using
                        indices. In fact, an old version of the class I pulled the example out of
                        did use the push method. Can't remember why I changed it, but it's going

                        • 9. Re: Extending Array class
                          Pop_Clingwrap Level 1
                          Cheers JPK that works well. I am confused about the super() statement though as if you remove it from the last example it still works.
                          The help file suggests that you should be able to use:
                          functio Array2(){

                          but if I do and then create an instance with ...new Array2(1,2,3,4);
                          it makes an array with one node only containing the string "1,2,3,4"

                          This is all beside the point really as I can now make it work at least (thank again everyone) but I would like to understand exactly what is going in the background here so please keep posting any findings.

                          NIce nice nice :)
                          • 10. Re: Extending Array class
                            Level 7
                            arguments is an array, so when you use super(arguments), the super class is
                            receiving only one argument -- the arguments array from the subclass. You
                            end up with an array with only one element at index 0 (the original
                            arguments array).

                            • 11. Re: Extending Array class
                              Peter Lorent Level 2
                              Raymond: sharp!

                              Pop Clingwrap: the call to super() can be omitted here because you don't pass anything to the constructor of the super class. If you omit the call, the compiler will make the call to super() automatically.