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