/// 实例方法装饰器 import 'reflect-metadata'; function decorator( target, // 对于静态成员来说是类的构造器,对于实例成员来说是类的原型链 methodName, // 指向方法名 descriptor: PropertyDescriptor // 描述符 ) { console.info({ target, methodName, descriptor }); // 某些场景下需要保存原来的方法 const oldValue = descriptor.value; // 可以通过修改descriptor.value的方式来改变方法 descriptor.value = function (...args) { oldValue.apply(this, args); console.log('@@'); }; // 如果返回了值,它会被用于替代属性的描述器。 } class Animal { @decorator call() { console.log('~~'); } @decorator static call() { console.log('~~'); } } const animal = new Animal(); animal.call(); // 输出 '~~\n@@' Animal.call(); // 输出 '~~\n@@' export default Animal;