Custom class that returns an array given start and end parameters in javascript

I want to create a "custom array" that takes in a start and end index and generates an array from that. My knowledge of classes in javascript is fairly limited, but I know Java fairly well. I couldn’t figure out how to extend a class (I kept getting an error) so what I came up with is the following:

class RangedArray{
constructor(startNum, endNum){
    this.startNum = startNum;
    this.endNum = endNum;
    this.timeslot = this.createTimeslot();
}

createTimeslot(){
    let index = 0;
    let arr = [];
    for (let num = this.startNum; num <= this.endNum; num++) {
        arr[index] = num;
        index++;
    }
    return arr;
}
}

This code does what I want, however if I want to access my array I would have to do something like this

let obj = new RangedArray(4, 21);
let timeslot1 = obj.timeslot;

What would be ideal is to do this in a single line. By that I mean I want to be able to use it like

let obj = new RangedArray(4, 21);
obj[0] = 1;

Like I said I think this can be done by extending the Array class but I’m not sure how to do that. I would basically want to override the regular array constructor inside the RangedArray class. So what would I have to do to get this functionality?

40 thoughts on “Custom class that returns an array given start and end parameters in javascript”