流浪のwolf

卷帝

导航

nextjs 类装饰器

// 类装饰器  == 本质就是一个函数 
// 在某个类的上面使用  @
// 可以等价于函数调用 doc(Zlx)
// 不会破坏原有的类 可以扩展类
//  "experimentalDecorators": true,  需要配置这个东西
const doc:ClassDecorator = (target:any) => {
    console.log(target);
    target.prototype.name =  "朱龙旭";
    target.prototype.age =  27;
}

@doc
class Zlx {
    constructor() {
    }
}

const z1:any = new Zlx();
console.log(z1.name);

运行 ts 的内容需要 ts-node 引擎:

tsc --init 生成一个 tsconfig.json 配置文件:

posted on 2024-04-03 21:07  流浪のwolf  阅读(13)  评论(0编辑  收藏  举报