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

171
Views
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 answers
Answer question

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 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!