/** https://www.zhihu.com/question/68257128?answer_deleted_redirect=true */ function ClassDecorator(path: string) { return function (target: Function) { !target.prototype.$Meta && (target.prototype.$Meta = {}) target.prototype.$Meta.baseUrl = path; debugger; }; } function MethodDecorator(url: string) { return function (target, methodName: string, descriptor: PropertyDescriptor) { !target.$Meta && (target.$Meta = {}); target.$Meta[methodName] = url; debugger; } } function StaticMethodDecorator(url: string) { return function (target, methodName: string, descriptor: PropertyDescriptor) { debugger; } } function ParameterDecorator(paramName: string) { return function (target, methodName: string, paramIndex: number) { !target.$Meta && (target.$Meta = {}); target.$Meta[paramIndex] = paramName; console.log('ParameterDecorator:', paramName); debugger; } } function StaticParameterDecorator(paramName: string) { return function (target, methodName: string, paramIndex: number) { console.log('StaticParameterDecorator:', paramName); debugger; } } function PropertyDecorator(value: string) { //属性描述符不会做为参数传入属性装饰器 //属性描述符只能用来监视类中是否声明了某个名字的属性。 return function (target: any, propertyName: string) { target[propertyName] = value; console.log('PropertyDecorator:', value); debugger; } } function StaticPropertyDecorator(value: number) { return function (target: any, propertyName: string) { console.log('StaticPropertyDecorator:', value); debugger; } } /** * typescript 1.5 * 装饰器 * 类、属性、方法、方法参数、getter/setter * 执行顺序 * 1、 */ @ClassDecorator('/hello') export class DecoratorExample { @PropertyDecorator('hello,world') greeting: string; @StaticPropertyDecorator(100) static hp: number; @MethodDecorator("http://www.baidu.com") execute( @ParameterDecorator("userId") userId: string, @ParameterDecorator("userId2") userId2: string) { } @StaticMethodDecorator("http://www.baidu.com") static show( @StaticParameterDecorator("userId") userId: string, @StaticParameterDecorator("userId2") userId2: string) { } } console.log(DecoratorExample.prototype['$Meta']); console.log(new DecoratorExample().greeting);