Forum Discussion

Tom_Welch's avatar
Tom_Welch
New Contributor
11 years ago

Inheriting from Array not working as expected (JScript)

I have the following code, which appears to be completely valid in a browser. It is an enhanced array that implements some of the features that Test Complete's antiquated version of JScript does not support - [].forEach(), for instance. This is the opening part of the constructor:






SuperArray.prototype = new Array();

SuperArray.prototype.constructor = SuperArray;





function SuperArray()

{

    if(arguments.length > 0)

    {

        if(Object.prototype.toString.call(arguments[0]) == '[object Array]' && arguments.length == 1)

        {

            this.parseArray(arguments[0]);

        }

        else

        {

            for(var j = 0; j < arguments.length; j++)

            {

                this.push(arguments);

            }

        }



    }

 

 ...

 

}



When the argument 'array' is pushed into the array object SuperArray extends, it does not increase the length of the array object (length is always 0). Every call to 'push()' simply overwrites the last variable (you get [0] = first arg, [0] = second arg... [0] = nth arg), and all the while, length remains 0 (even though it should really be at least 1). When I inspect an instance of SuperArray(), it shows none of the methods declared within its constructor.



What do I need to do to get this to work (I would prefer not to have a highly inelegant solution like simply including a normal array instance as a member of SuperArray, and modifying that)?



 


  • Even if you do extend the array prototype it will only apply in that single unit.



    What will work is defining iteration functions (that can be global) that take an array argument. 



    For instance I have a MAP function that I use as a general iterator, stolen from 

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map




    function MAP(arr, callback, thisArg) {


        var T, A, k;


        if (arr == null) {


            return false;


        }


        var O = Object(arr);


        var len = O.length >>> 0;


        if (typeof callback != "function") {


          throw new TypeError(callback + " is not a function");


        }


        if (thisArg) {


          T = thisArg;


        }


        A = new Array(len);


        k = 0;


        while(k < len) {


          var kValue, mappedValue;


          if (k in O) {


            kValue = O[ k ];


            mappedValue = callback.call(T, kValue, k, O);


            A[ k ] = mappedValue;


          }


          k++;


        }


        return A;


    }


     

  • Tom_Welch's avatar
    Tom_Welch
    New Contributor
    I don't think this is correct. I can and have used defintions like this from separate modules in TC 10. If you create a constructor, and specify the inheritance chain, you can use the constructor from other modules. If you want to modify a built in 'class' like Array, you need to create an instance of that class within the module (in terms of scope, it needs to be an object on that module, rather than within a function/ object)  that defines the prototype modification (var arr = [];) and then you should be able to use it from other modules. I don't understand why the modifcation isn't working in this instance.