Calling map() on an instance text of ConsoleText class that extends Array results in a ConsoleText(text.length) call (constructor input: 2 in the last line).
How to fix it so that text.map() calls Array constructor instead? Or it is a feature of inheritance?
EDIT: found an answer here
'use strict'
let initializingText = true;
class ConsoleText extends Array {
constructor(strings) {
console.log('constructor input:', strings);
if (initializingText) {
super(...strings);
} else {
super('item1', 'item2', 'item3', 'item4', 'item5');
}
}
}
let text = new ConsoleText(['a', 'b']); // constructor input: (2) ['a', 'b']
console.log('text:', text); // text: ConsoleText(2) ['a', 'b']
initializingText = false; // switch to escape error destructuring a number
console.log(text.map(() => 'q')); // constructor input: 2
// ConsoleText(5) ['q', 'q', 'item3', 'item4', 'item5']
Arrayone. You can simply overwriteArray.prototype.mapwith your own implementation, it's a really simple method anyway.