Is it possible to subclass and inherit from javascript Arrays?
I\'d like to have my own custom Array object that has all the features of an Array, but contains addit
I've created a simple NPM module that solves this - inherit-array. It basically does the following:
function toArraySubClassFactory(ArraySubClass) {
ArraySubClass.prototype = Object.assign(Object.create(Array.prototype),
ArraySubClass.prototype);
return function () {
var arr = [ ];
arr.__proto__ = ArraySubClass.prototype;
ArraySubClass.apply(arr, arguments);
return arr;
};
};
After writing your own SubArray class you can make it inherit Array as follows:
var SubArrayFactory = toArraySubClassFactory(SubArray);
var mySubArrayInstance = SubArrayFactory(/*whatever SubArray constructor takes*/)