Instructor: So we now saw a more useful decorator.
I wanna conclude this very basic introduction to decorator
with another important thing you should know,
you can add more than one decorator to a class,
or anywhere else where you can use a decorator.
So let's also add
the logger decorator here for example, like this.
If we save that, you see it compiles without errors,
and indeed we see the log message here as well.
This just leaves one important question,
in which order do these decorators execute?
Well for that, to find out,
let's throw log messages into both.
We do have a log message in the Logger, obviously,
now let's go to the decorator function.
So to the inner function of WithTemplate,
and say Rendering Template, for example.
And now let's see
in which order these functions are executing
and in which order these log statements are printed.
And we see Rendering Template runs first,
and then we get the Logger output.
Now what this tells us of course,
is that they execute bottom up.
The bottom-most decorator first,
then thereafter, the decorators above it.
WithTemplate runs first, then Logger executes.
And important,
I'm talking about the actual decorator functions.
The decorator factories here run earlier.
If we have Logger Factory here,
and we have the same log in the Template Factory,
so there I say Template Factory in my log,
you will see that there,
actually the Logger Factory runs first,
and then we got the Template Factory.
And that makes sense because in the end,
even though we got this @ symbol here,
here I'm executing a function,
and we're doing the same here.
And of course, regular JavaScript rules apply here
and this function execution
happens before this function execution.
Which is why we see the Logger Factory
before we see Template Factory.
So the creation of our actual decorator functions
happens in the order
in which we specify these factory functions.
But the execution of the actual decorator functions
then happens bottom up.
Which means the bottom-most decorator executes first,
so this decorator function,
and thereafter the decorator above that executes.
It's just something you have to know.
With that, we got a solid base knowledge about decorators,
and you also see a more advanced decorator
which hopefully, also gives you a first impression
of why decorators can be useful,
especially if you also think about frameworks like Angular.
Now let's see to which other places, besides classes,
we can add decorators
and what we might be able to do there.