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

152
Visualizações
Uexpected Function Behavior. JS Classes

I've created HTML builder (finally!)

It just has methods: create div, span, p, br.

As you can see in console.logs it has nesting and chaining behavior.

And for detecting nesting and chaining I have item instanceof Class
But it doesn't show me correct return when I have nesting condition.

Need help to find the mistake and get output in first console.log =

<div><p>Hello</p><p>World</p></div>

class Templater {
    constructor() {
        this.arr = []; 
        this.nesting = false;
    }
    transform(tags) {
        return tags.join("");
    }

    div(...tags) {
       tags.forEach(item => {
            this.nesting = (item instanceof Templater);
        });
        this.arr.push(`<div>${this.transform(tags)}</div>`) 
        return this;
    }

    span(...tags) {
        tags.forEach(item => {
            this.nesting = (item instanceof Templater);
        });
        this.arr.push(`<span>${this.transform(tags)}</span>`);
        return this
    }
    br(argument) {
        tags.forEach(item => {
            this.nesting = (item instanceof Templater);
        });
        if (argument) {
            throw new Error('Nested content is not allowed');
        } else {
            this.arr.push(`<br>`);
           
            return this; 
        }
    }
    p(...tags) {
        tags.forEach(item => {
            this.nesting = (item instanceof Templater);
        });
        this.arr.push(`<p>${this.transform(tags)}</p>`);
        return this
    }

    toString() {
        if (this.nesting) {
            this.nesting = false; 
            let qwe = [...this.arr]; 
            this.arr = []; 
            return qwe[qwe.length-1];
        } else {
            let qwe = [...this.arr]; 
            this.arr = [];
           return qwe.join('');
        }
    }

}
const template = new Templater();

console.log(template.div(
    template.p('Hello'),
    template.p('World')
).toString());

console.log(template.div().toString());
console.log(template.div('Hello').p('fix').toString());

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Worked for me with :

    console.log(template.div(
        template.p('Hello').p('World').toString()
    ).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