/// 属性装饰器 import 'reflect-metadata'; const formatMetadataKey = Symbol('format'); function format(formatString: string) { return ( target, // 指向实例原型 propertyKey // 指向属性名称 ) => { console.info({ target, propertyKey }); return Reflect.metadata(formatMetadataKey, formatString)( target, propertyKey ); }; } class Animal { @format('Hello, %s') message: string; constructor() { this.message = 'message'; } call() { const formatString = Reflect.getMetadata( formatMetadataKey, this, 'message' ); console.log(formatString.replace('%s', this.message)); } } const ani = new Animal(); ani.call(); export default Animal;