- Mastering TypeScript 3
- Nathan Rozentals
- 130字
- 2021-07-02 12:42:57
Multiple decorators
Multiple decorators can be applied one after another to the same target. As an example of this, consider the following code:
function secondDecorator(constructor : Function) { console.log('secondDecorator called.') } @simpleDecorator @secondDecorator class ClassWithMultipleDecorators { }
Here, we have created another decorator named secondDecorator, which also simply logs a message to the console. We are then applying both the simpleDecorator (from our earlier code snippet) and the secondDecorator decorators to the class definition of the class named ClassWithMultipleDecorators. The output of this code is as follows:
secondDecorator called. simpleDecorator called.
The output of this code shows us an interesting point about decorators. They are called in reverse order of their definition.
Decorators are evaluated in the order they appear in the code, but are then called in reverse order.