1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
function propDecorator1(targetPrototype: any, propName: string) { console.log("属性装饰器 1:", targetPrototype, propName); }
function propDecorator2(params: string) { console.log("属性装饰器 2 before:", params); return function (targetPrototype: any, propName: string) { console.log( "属性装饰器 2:", targetPrototype, propName, targetPrototype[propName] ); targetPrototype[propName] = "会在原型上面添加该属性并赋值,不是实例对象"; }; }
function propDecorator3(params: string) { console.log("属性装饰器 3 before:", params); return function (targetPrototype: any, propName: string) { console.log("属性装饰器 3:", targetPrototype, propName); }; }
function methodDecorator1( targetPrototype: any, methodName: string, descriptor: PropertyDescriptor ) { console.log("方法装饰器 1:", targetPrototype, methodName, descriptor); }
let methodDecorator2: any = function (params: string) { console.log("方法装饰器 2 before:", params); return function ( targetPrototype: any, methodName: string, descriptor: PropertyDescriptor ) { console.log("方法装饰器 2:", targetPrototype, methodName, descriptor); }; };
let methodDecorator3: any = function (params: string) { console.log("方法装饰器 3 before:", params); return function ( targetPrototype: any, methodName: string, descriptor: PropertyDescriptor ) { console.log("方法装饰器 3:", targetPrototype, methodName, descriptor); let originalMethod = descriptor.value; descriptor.value = function (...args: any[]) { args = args.map((item) => (item += " test")); return originalMethod.apply(this, args); }; return descriptor; }; };
function paramDecorator1( targetPrototype: any, methodName: string, paramIndex: number ) { console.log("方法参数装饰器 1:", targetPrototype, methodName, paramIndex); }
function paramDecorator2(params: string) { console.log("方法参数装饰器 2 before:", params); return function ( targetPrototype: any, methodName: string, paramIndex: number ) { console.log("方法参数装饰器 2:", targetPrototype, methodName, paramIndex); }; }
function paramDecorator3(params: string) { console.log("方法参数装饰器 3 before:", params); return function ( targetPrototype: any, methodName: string, paramIndex: number ) { console.log("方法参数装饰器 3:", targetPrototype, methodName, paramIndex); }; }
function staticPropDecorator(param: string) { console.log("静态属性修饰器 before:", param); return function (targetPrototype: any, propName: string) { console.log( "静态属性修饰器:", targetPrototype, propName, targetPrototype[propName] ); targetPrototype[propName] = "静态属性初始值被修改了!"; }; }
function staticMethodDecorator(param: string) { console.log("静态方法修饰器 before:", param); return function ( targetPrototype: any, methodName: string, descriptor: PropertyDescriptor ) { console.log("静态方法修饰器:", targetPrototype, methodName, descriptor); }; }
function classDecorator1(constructor: Function) { console.log("类装饰器 1:", constructor); }
function classDecorator2(params: string) { console.log("类装饰器 2 before:", params); return function (constructor: Function) { console.log("类装饰器 2:", constructor); }; }
function classDecorator3(params: string) { console.log("类装饰器 3 before:", params); return function (constructor: Function) { console.log("类装饰器 3:", constructor); }; }
function accessorDecorator(target: any, propertyKey: string, descriptor: PropertyDescriptor) { descriptor.get = () => "存取器修饰后的值" }
@classDecorator1 @classDecorator2("params2") @classDecorator3("params3") class Test { @propDecorator1 @propDecorator2("param2") @propDecorator3("param3") public msg: string = "属性初始值";
@staticPropDecorator("静态属性") static title: string = "静态属性初始值";
constructor(msg: string) { this.msg = msg; }
@methodDecorator1 @methodDecorator2("param2") @methodDecorator3("param3") toString( @paramDecorator1 str1: string, @paramDecorator2("param2") str2: string, @paramDecorator3("param3") str3: string ) { console.log("toString:", str1, str2, str3); }
@staticMethodDecorator("静态方法") static staticToString() { console.log(this.title); }
@accessorDecorator get msg1() { return this.msg }
}
let t: any = new Test("this is a msg."); t.toString("ss", "dd", "ff"); console.log(t.msg, t.proto, t.msg, t.msg1); Test.staticToString();
|