参数装饰器

  1. function logPosition(target: any, propertyKey: string, parameterIndex: number) {
  2. console.log(parameterIndex);
  3. }
  4. class Cow {
  5. say(b: string, @logPosition c: boolean) {
  6. console.log(b);
  7. }
  8. }
  9. new Cow().say('hello', false); // outputs 1 (newline) hello

上面演示了装饰方法的参数。熟悉Angular 2的读者现在可以想象Angular 2如何实现他们的@Inject()系统。