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

119
Views
Orden de ejecución de funciones JS / reconstrucción de lógica de clase JS

Necesito que la salida sea:

 <p>123<span>321</span></p>

Pero ahora eso es:

 <span>321</span><p>123<span>321</span></p>

¿Cuál es la mejor opción para comenzar a ejecutar p() y luego span() dentro de él?

 class Templater { constructor() { this.output = ''; } span(...tags) { const transform = tags => tags.join(''); this.output += `<span>${transform(tags)}</span>` return this; } p(...tags) { const transform = tags => tags.join(''); this.output += `<p>${transform(tags)}</p>` return this; } toString() { return this.output; } } const template = new Templater(); console.log(template.p('123', template.span('321')).toString());

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Lo más fácil es simplemente omitir todo el asunto this.output por completo y simplemente confiar en la pila de llamadas.

 class Templater { _transform(tags) { return tags.join(""); } span(...tags) { return `<span>${this._transform(tags)}</span>`; } p(...tags) { return `<p>${this._transform(tags)}</p>`; } } const template = new Templater(); console.log(template.p('123', template.span('321')).toString());

Otra opción es construir un árbol de objetos que sepan cómo convertirse en una cadena:

 class Tag { constructor(tag, children) { this.tag = tag; this.children = children; } toString() { const children = this.children.map(c => c.toString()).join(""); return `<${this.tag}>${children}</${this.tag}>`; } } class Templater { span(...children) { return new Tag('span', children); } p(...children) { return new Tag('p', children); } } const template = new Templater(); console.log(template.p('123', template.span('321')).toString());

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!