Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

83
Views
Sobrescribir cadena vacía en objeto Javascript

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)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!