I'm trying to use the map function inside the toString overwritten in my Array subclass and I'm seeing a strange behaviour. It looks like that .map is calling the constructor of my subclass for no apparent reason and throwing a TypeError.
class Seq extends Array {
constructor(seq = '') {
super();
(seq.match(/.{2}/g) || []).forEach((mi) => this.push(mi));
}
toString() {
return this.map(it=>it).join('');
}
}
new Seq('1643').toString() //expects to see '1643' as the output but getting a TypeError
Recreating the array in the toString method fixes the problem, but I'd like to understand why.
toString() {
return [...this].map(it=>it).join('');
}