+ 1
Can anyone explain the concept of function decorator in Javascript
Javascript functions
2 odpowiedzi
+ 12
Good question. It would be cool have more such and more concrete questions!
Decorator is a special function that takes another function and alters its behavior.
The idea is that we can call function-decorator for any function, and it will return the wrapper. That’s great, because we can have many functions that could use such a feature, and all we need to do is to apply function-decorator to them.
Some benefits of using these ones:
1. The decorator is reusable. We can apply it to another function.
2. The inner logic is separate, it didn't increase the complexity of callback function itself (if there were any).
3. We can combine multiple decorators if needed (other decorators will follow).
These functions are wrappers. You don't modificate original function.
If you didn't understand clearly I will try to give some code example (one instance can be caching function that wraps some separate CPU-heavy function).
0
Thanks sir for the explanation
Yes I'll like the code example too, it'll help