Can anyone explain the concept of function decorator in Javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone explain the concept of function decorator in Javascript

Javascript functions

18th Jul 2019, 10:50 PM
Oyewo Bolaji Sodiq
Oyewo Bolaji Sodiq - avatar
2 Answers
+ 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).
19th Jul 2019, 3:54 AM
Вап
0
Thanks sir for the explanation Yes I'll like the code example too, it'll help
19th Jul 2019, 5:15 AM
Oyewo Bolaji Sodiq
Oyewo Bolaji Sodiq - avatar