装饰器
命令行安装:
tsc --target ES5 --experimentalDecorators
配置:
// tsconfig.json{ "compilerOptions": { "target": "ES5", "experimentalDecorators": true } }
装饰器组合
function f() {console.log('f():evaluated');return function (target, propertyKey: string, descriptor: PropertyDescriptor) {console.log('f():called');}}function g() {console.log('g():evaluated');return function (target, propertyKey: string, descriptor: PropertyDescriptor) {console.log('g():called');}}class myC {@f()@g()method() { }}
类装饰器
@sealedclass Greeter{greeting:stringconstructor(message:string){this.greeting = message}greet(){return this.greeting;}}
