Very glad to learn of this way of basically subclassing a JavaScript Array
(code copied from link):
function SomeType() {
this.push(16);
}
SomeType.prototype = [];
SomeType.prototype.constructor = SomeType; // Make sure there are no unexpected results
console.log(new SomeType()); // Displays in console as [16]
But this isn't quite complete. Is there a way to fake subclass the Array like this and get the []
method?
var a = [];
a[3] = true;
console.log(a.length); //=> 4
var s = new SomeType();
s[3] = true;
console.log(s.length); //=> 1
This way you can still treat it as an array when doing a for loop:
for (var i = 0; i < s.length; i++) {
var item = s[i];
}