Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda