Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
Order of executing JS functions / rebuilding JS Class logic

I need output to be:

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

But now that's:

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

What is the best option to start execute p() and then span() inside it?

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 Respostas
Responde à pergunta

0

The easiest is to just skip the whole this.output thing altogether and just trust the call stack.

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());

Another option is to construct a tree of objects that know how to render themselves into a string:

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda