|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +@classDecorator |
| 4 | +class Sample { |
| 5 | + a(): string { |
| 6 | + return ""; |
| 7 | + } |
| 8 | + |
| 9 | + @methodDecorator |
| 10 | + b(): string { |
| 11 | + return ""; |
| 12 | + } |
| 13 | + |
| 14 | + @staticMethodDecorator |
| 15 | + static c(): string { |
| 16 | + return ""; |
| 17 | + } |
| 18 | + |
| 19 | + @propertyDecorator |
| 20 | + d = ""; |
| 21 | + |
| 22 | + |
| 23 | + @accessorDecorator |
| 24 | + get e(): string { |
| 25 | + return ""; |
| 26 | + } |
| 27 | + |
| 28 | + f( @parameterDecorator str: string): string { |
| 29 | + return `Hello, ${str}`; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +console.log("------"); |
| 34 | + |
| 35 | +let obj = new Sample(); |
| 36 | +console.log(obj.a()); |
| 37 | +console.log(obj.b()); |
| 38 | +console.log(Sample.c()); |
| 39 | +console.log(obj.d); |
| 40 | +console.log(obj.e); |
| 41 | +console.log(obj.f("parameter")); |
| 42 | + |
| 43 | +function classDecorator(sampleClazz: typeof Sample): typeof Sample { |
| 44 | + console.log("classDecorator", arguments); |
| 45 | + sampleClazz.prototype.a = function() { |
| 46 | + return "Hello from classDecorator!"; |
| 47 | + } |
| 48 | + return null; |
| 49 | +} |
| 50 | + |
| 51 | +function methodDecorator(prototypeOfSample: any, key: string, propertyDescription: PropertyDescriptor): PropertyDescriptor { |
| 52 | + console.log("methodDecorator", arguments); |
| 53 | + return null; |
| 54 | +} |
| 55 | + |
| 56 | +function staticMethodDecorator(sampleClazz: typeof Sample, key: string, propertyDescription: PropertyDescriptor): PropertyDescriptor { |
| 57 | + console.log("staticMethodDecorator", arguments); |
| 58 | + return null; |
| 59 | +} |
| 60 | + |
| 61 | +function propertyDecorator(prototypeOfSample: any, key: string): void { |
| 62 | + console.log("propertyDecorator", arguments); |
| 63 | +} |
| 64 | + |
| 65 | +function accessorDecorator(prototypeOfSample: any, key: string, propertyDescription: PropertyDescriptor): PropertyDescriptor { |
| 66 | + console.log("accessorDecorator", arguments); |
| 67 | + return null; |
| 68 | +} |
| 69 | + |
| 70 | +function parameterDecorator(prototypeOfSample: any, methodName: string, parameterIndex: number): void { |
| 71 | + console.log("parameterDecorator", arguments); |
| 72 | +} |
0 commit comments