0

I need that child class StringBuilder would pass the string to the parent class and call the minus method. The following code does not work unless I won't hardcode the string in the constructor. With numbers this works just fine. Why doesn't it overwrite the string? Or maybe I'm doing everything completely wrong?

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)
6
  • Your Builder constructor does not take any parameters. Declare a parameter and assign that parameter to this.str. Commented May 1, 2022 at 19:14
  • @Mushroomator By the assignment, if no string is passed, it should take empty string by default. Moreover, this.str.slice throws an error of undefined Commented May 1, 2022 at 19:16
  • 1
    This looks like a bad inheritance scheme. A StringBuilder should not have a .int property, and an IntBuilder should not have a .str property. Commented May 1, 2022 at 19:21
  • @Bergi, well, I agree with you. But it must be one class for actions with both integers and strings. I don't know how to make common methods for both classes so that integers wont inherit strings and vice versa Commented May 1, 2022 at 19:26
  • 1
    "it must be one class for actions for both integers and strings" - no it doesn't? Both builders may share an interface, i.e. both implement a minus method, but that doesn't mean both have to inherit from the same class. Commented May 1, 2022 at 19:28

1 Answer 1

2

Your Builder constructor does not take any parameters. Declare a parameter and assign that parameter to this.str. You can use default parameters to ensure it will be initialized to whatever you want even when the constructor is called without parameters.

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)

Sign up to request clarification or add additional context in comments.

1 Comment

OMG, I thought I can't assign default values in brackets :D Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.