4. Method Decorator
# Typescript Decorator
Medium
At the beginning, we introduced the principle of decorators, which is actually the original implementation of method decorators. Unlike property decorators, method decorators accept three parameters.
One thing to note when overloading method decorators is that the value must be defined using a function, not an arrow function. This is because we use this when calling the original old method, such as method.apply(this, args), and the this here needs to be defined using a function.
Parameters
target For static members, it is the constructor of the class; for instance members, it is the prototype object of the class. key The name of the method. descriptor: PropertyDescriptor The property descriptor of the method (the most important parameter).
Attribute descriptor
The attribute description includes the following attributes:
-
configurable?: boolean; Whether it can be deleted, whether method characteristics can be modified, or whether accessor properties can be modified.
-
enumerable?: boolean; Whether it exists when iterating through objects.
-
value?: any; Used to define a new method to replace the old method.
-
writable?: boolean; Whether it is writable.
-
get?(): any; Accessor.
-
set?(v: any): void; Accessor.
Next, we use method decorators to modify the login logger in the original decorator principle.