2

I have an application using simple arrays (Array) and typed arrays (TypedArray).

I developed some needed extensions to the array types like (min, max, sum, ...).

But here's the tricky part: how define the created functions for all arrays in javascript?

If was some inheritance hierarchy between all of them, this problem would be more simpler. But so far I found no greater parent class.

For now I'm doing this:

// MIN FUNCTION
Array              .prototype.min =
Int8Array          .prototype.min =
Int16Array         .prototype.min =
Int32Array         .prototype.min =
Uint8Array         .prototype.min =
Uint16Array        .prototype.min =
Uint32Array        .prototype.min =
Uint8ClampedArray  .prototype.min =
Float32Array       .prototype.min =
Float64Array       .prototype.min = function () {
    // my code;
}

// MAX FUNCTION
Array              .prototype.max =
Int8Array          .prototype.max =
Int16Array         .prototype.max =
Int32Array         .prototype.max =
Uint8Array         .prototype.max =
Uint16Array        .prototype.max =
Uint32Array        .prototype.max =
Uint8ClampedArray  .prototype.max =
Float32Array       .prototype.max =
Float64Array       .prototype.max = function () {
    // my code;
}

// GO ON...

This is so monstrously ugly to look that I want to rip my eyes out.

How can I improve that? There is something to connect all these types to be used?

EDITED QUESTION:
How can I write this code without explicitly writing all types of javascript array?

Jonny Piazzi
  • 3,684
  • 4
  • 34
  • 81
  • The TypedArrays don't share a prototype? _Also: I suggest adding the `typed-arrays` tag._ – evolutionxbox Feb 10 '17 at 17:39
  • 3
    Why don't you use a function that you pass your array into? E.g. running `Math.max.apply(null, yourArrayOfTypeX);` works just fine. – schroffl Feb 10 '17 at 17:40
  • Make a function that receives the method you want to add and its name as a string, and iterates the `prototype` objects assigning the method to each prototype using that name. –  Feb 10 '17 at 17:40
  • It does seem that the `__proto__` of most of those prototypes is shared, but I don't know if that'll be reliable. –  Feb 10 '17 at 17:41
  • Not sure what issue is? You have already written the extensions. – guest271314 Feb 10 '17 at 17:47
  • What issue is: This is not a problem of broken code, but a "coding good practices" problem or "how to clean hard code" problem. – Jonny Piazzi Feb 10 '17 at 18:11
  • Note that if you have actual working code and just want to know "how to code it better", you might be able to ask this on [codereview.se], but *please* read their help center before posting to ensure it's on topic. – Heretic Monkey Feb 10 '17 at 18:36
  • Also, beware of extending native objects: http://stackoverflow.com/a/14034242/215552 – Heretic Monkey Feb 10 '17 at 18:36

2 Answers2

3

This appears to work:

function arMax(){
    var len = this.length;
    var i;
    var max=-Infinity;
    for(i=0;i<len;i++)
        if(this[i]>max)
            max=this[i];
    return max;
}
function arMin(){
    var len = this.length;
    var i;
    var min=+Infinity;
    for(i=0;i<len;i++)
        if(this[i]<min)
            min=this[i];
    return min;
}
for(tp of [ 
    Array, 
    Int8Array, 
    Int16Array,
    Int32Array,
    Uint8Array,
    Uint16Array,
    Uint32Array,
    Uint8ClampedArray,
    Float32Array,
    Float64Array,
]){
    tp.prototype.max = arMax;
    tp.prototype.min = arMin;
}

console.log([ 2, 34, 3, 2, -43, -1 ].max())
console.log([ 2, 34, 3, 2, -43, -1 ].min())
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • 1
    This is a good answer +1, improves a lot but not solve the 'explicitly writing all types' part. – Jonny Piazzi Feb 10 '17 at 18:08
  • @JonnyPiazzi There's only a few of those types. It shouldn't be a big deal to type them out. You usually do this kind of thing at all the lower levels (C,C++). Slapping the function on Object.prototype works, but I guess that's a little bit too much. I'd type it out and move on (unless a better answer comes up). – Petr Skocik Feb 10 '17 at 18:22
0

If you are going to us this in browser, then you can get a list of available arrays from window object itself.

var availableAr = [];
Object.getOwnPropertyNames(window).forEach(function(name){ 
 if( name.toString().indexOf('Array')!== -1 ){
       availableAr.push( name.toString() )
    }
})
console.log(availableAr);
console.log( 
        new window[ availableAr[0] ]()
  );
Jimish Fotariya
  • 1,004
  • 8
  • 21