const deprecated = (warnText=`The interface is deprecated`) =>(target, key, descriptor) => { if (typeof descriptor.value !== 'function') { thrownewError('The @deprecated decorator can only be used on function.'); }
new Test().calculate() // The interface is deprecated
统计方法执行时间
对于复杂的操作可能会需要记录操作执行的时间:
const runtime = (target, key, descriptor) => { if (typeof descriptor.value !== 'function') { thrownewError('The @runtime decorator can only be used on function.'); }
label = `The function ${key} running used`
return { ...descriptor, value: function() { console.time(label); let result = descriptor.value.apply(this, arguments); console.timeEnd(label); return result; } } }
classTest{ @runtime calculate() { for(var i = 0; i < 10000000; i ++) {} } }
new Test().calculate() // The function calculate running used: 8.162841796875ms