装饰器

  • 命令行安装:

    1. tsc --target ES5 --experimentalDecorators
  • 配置:

    1. // tsconfig.json
    2. { "compilerOptions": { "target": "ES5", "experimentalDecorators": true } }

    装饰器组合

    1. function f() {
    2. console.log('f():evaluated');
    3. return function (target, propertyKey: string, descriptor: PropertyDescriptor) {
    4. console.log('f():called');
    5. }
    6. }
    7. function g() {
    8. console.log('g():evaluated');
    9. return function (target, propertyKey: string, descriptor: PropertyDescriptor) {
    10. console.log('g():called');
    11. }
    12. }
    13. class myC {
    14. @f()
    15. @g()
    16. method() { }
    17. }

    类装饰器

    1. @sealed
    2. class Greeter{
    3. greeting:string
    4. constructor(message:string){
    5. this.greeting = message
    6. }
    7. greet(){
    8. return this.greeting;
    9. }
    10. }