TypeScript-参数装饰器

参数装饰器

  • 参数装饰器写在一个参数声明之前(紧靠着参数声明)

参数装饰器表达式会在运行时当作函数被调用,会自动传入下列 3 个参数:

  • 对于静态成员来说是当前的类,对于实例成员是当前实例
  • 参数所在的方法名称
  • 参数在参数列表中的索引

实例成员:

function test(target: any, proptyName: string, index: number) {
    console.log(target);
    console.log(proptyName);
    console.log(index);
}

class Person {
    say(age: number, @test name: string): void {

    }
}

image-20211206143245696

静态成员:

function test(target: any, proptyName: string, index: number) {
    console.log(target);
    console.log(proptyName);
    console.log(index);
}

class Person {
    say(age: number, name: string): void {

    }

    static hi(@test val: string) {

    }
}

image-20211206143446102

无案例🐤

其它题外话

  • 属性装饰器, 参数装饰器最常见的应用场景就是配合 元数据(reflect-metadata)
  • 在不改变原有结构的同时添加一些额外的信息
  • 但是元数据目前也是在提案中, 也还没有纳入正式的标准
  • 所以对于装饰器而言, 我们只需要了解即可
  • 因为提案中的所有内容将来都是有可能被修改的
  • 因为提案中的所有内容目前都有兼容性的问题
posted @ 2021-12-06 14:46  BNTang  阅读(209)  评论(0编辑  收藏  举报