Necesito que la clase secundaria StringBuilder pase la cadena a la clase principal y llame al método menos. El siguiente código no funciona a menos que no codifique la cadena en el constructor. Con números esto funciona bien. ¿Por qué no sobrescribe la cadena? ¿O tal vez estoy haciendo todo completamente mal?
class Builder { constructor() { this.int = 0 this.str = '' } minus(...n) { this.int = n.reduce((sum, current) => sum - current, this.int) this.str = this.str.slice(0, -n) return this } } class IntBuilder extends Builder { constructor(int) { super(int) } } class StringBuilder extends Builder { constructor(str) { super(str) } } let number = new IntBuilder() number.minus(100, 99) console.log(number) let string = new StringBuilder('Hello') string.minus(2) console.log(string)Su constructor Builder no toma ningún parámetro. Declare un parámetro y asigne ese parámetro a this.str . Puede usar los parámetros predeterminados para asegurarse de que se inicialice como desee, incluso cuando se llame al constructor sin parámetros.
class Builder { constructor(str = "") { this.int = 0 this.str = str } minus(...n) { this.int = n.reduce((sum, current) => sum - current, this.int) this.str = this.str.slice(0, -n); return this; } } class IntBuilder extends Builder { constructor(int) { super(int) } } class StringBuilder extends Builder { constructor(str) { super(str) } } let number = new IntBuilder() number.minus(100, 99) console.log(number) let string = new StringBuilder('Hello') string.minus(2) console.log(string)