函数装饰器
function testDecorator(isTrue: boolean){
console.log("111111111")
if(isTrue){
return function(class_name: any){
console.log("2222222")
console.log(class_name)
class_name.prototype.getName= () => {
console.log('dell');
};
}
}else{
return function(class_name: any){}
}
}
function decorator(class_name: any){
console.log("333333333")
console.log(class_name)
class_name.prototype.getName= () => {
console.log("44444444")
console.log('dell~');
}
};
@testDecorator(true)
@decorator
class AAA {
constructor(){console.log('aaa')}
}
const aaa = new AAA();
(aaa as any).getName()
上边的变形
function testDecorator() {
return function<T extends new (...args: any[]) => any>(constructor: T) {
return class extends constructor {
name = 'lee';
getName() {
return this.name;
}
};
};
}
const Test = testDecorator()(
class {
name: string;
constructor(name: string) {
this.name = name;
}
}
);
const test = new Test('dell');
console.log(test.getName());