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?

8
  • The TypedArrays don't share a prototype? Also: I suggest adding the typed-arrays tag. Commented Feb 10, 2017 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. Commented Feb 10, 2017 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. Commented Feb 10, 2017 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. Commented Feb 10, 2017 at 17:41
  • Not sure what issue is? You have already written the extensions. Commented Feb 10, 2017 at 17:47

2 Answers 2

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())

Sign up to request clarification or add additional context in comments.

2 Comments

This is a good answer +1, improves a lot but not solve the 'explicitly writing all types' part.
@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).
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] ]()
  );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.