代码:
function enhance(target, key, descriptor) { const method = descriptor.value; let extraSpeed = 50; let finalSpeed; descriptor.value = (...args) => { console.log(target === Car.prototype); // true console.log(key); // 'drive' console.log(descriptor); // 属性描述符 args[0] += extraSpeed; finalSpeed = method.call(target, args); return finalSpeed; } return descriptor;}class Car { @enhance drive(speed) { console.log(`时速:${speed}`); }}let car = new Car('买菜车');car.drive(100);
运行环境
TypeScript官网对方法装饰器对描述