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

172
Visualizações
How to build a proper decorator pattern or factory for Javascript

I am trying to build decorator pattern that it should get 2 parameters. First parameter is the function that should be decorated and second is the array of functions that should be applied to the main function.

For example:

const decoratedFunc = decorate(shouldDecoratedFunc, [validate, confirmMessage("Are you sure")]
 
function shouldDecoratedFunc(a, b, c) {
    return a + b + c;
}

The problem is that when I build the decorate function I just can't pass the parameters of the function that should be decorated to the decorate function. So I really don't know what to do. Or how to achieve this.

Can anyone help me who build something like this?

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

0

Best I could do is below and it is actually works.

The problem with this one is when I want to execute the function that decorated conditionally, I just can't.

function decorator(target, decorators) {
    const name = target.name
    const descriptor = Object.getOwnPropertyDescriptor(target, "prototype")
    return (...args) => {
        decorators.forEach(func => {
            if (func instanceof Function) func(target.bind(this, ...args) ,name, descriptor)
        })
        target.call(this, ...args)
    }
}

I'm open for any ideas

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