Beginning of back to normalcy I think. Back to writing more about Software Engineering and less about the dark road of fascist-like politics and conspiracies.
Technically the idea of decorators for node was first introduced with ES6 Decorators which took off. Not sure what happened with it though. Proposal rejected? Anyway.
The idea of decorators comes from annotations in Java where you can use reflection to get the annotation and get the associated metadata and then use that to construct what you wanted to construct.
Typescript has Decorators for class, method, property and parameters.
I've been afraid of trying to create them in general because I figured they were complicated. It was weird for me at first because I look at Java's way and reflection is used to get annotations. In Typescript, the decorator is a function that is executed! Far as I know, it is executed only once I think at the point of first object instantiation, but I could be wrong.
Now, you can do some weird shit with decorators such as wrapping functions, constructors, changing prototype parameters. These are things you cannot do with annotations in Java nor would I want to! I can see why decorators is the proper name for this feature though. Annotations is also proper in Java since they only attach information.
I created a test project to see how this stuff works.
- The order of execution of the decorators is parameter, method and class. Feels like a weird way to go, but ok maybe there is some history or context I can read more about to learn why this order.
- I was wondering how I could bring in an instance of an outside object into the target object to call functions, but decorators aren't the way to go with that the way I was thinking. Better create a template of what you'd like collect from the target object and then use that to apply onto the outside object instead.
- One could use symbols and attach them to the target class constructor/prototype or use Reflect.metadata to add metadata to the target class that you can retrieve anywhere at anytime. I was always with the thought of storing data using "registry" singletons in module scope or globals.
More Information about Decorators and their types: https://www.logicbig.com/tutorials/misc/typescript/class-decorators.html
Good example of decorator usage is for setting up routing with controller classes: https://nehalist.io/routing-with-typescript-decorators/
Not bad, it is good stuff. Not sure why I have been ignorant of this for so long.