基本类装饰器
类装饰器只是一个函数,它将类作为唯一的参数,并在对它执行某些操作后返回它:
function log<T>(target: T) {
// Do something with target
console.log(target);
// Return target
return target;
}
然后我们可以将类装饰器应用于类:
@log
class Person {
private _name: string;
public constructor(name: string) {
this._name = name;
}
public greet() {
return this._name;
}
}