具體查看 介紹https://tc39.github.io/propos...git
根據最新的api我寫了小demo以下:github
function decoratorclass (target) { if (target.kind !== 'class') { return } for (const element of target.elements) { if (element.kind !== 'method') { continue } const descriptor = element.descriptor const oldMethod = descriptor.value descriptor.value = (...args) => { console.time(element.key) const ret = oldMethod.apply(this, args) console.timeEnd(element.key) return ret } } } @decoratorclass class Numberic { number = '3.1415926' static name = 'Bob' add (...num) { return num.reduce((p, n) => (p + n), 0) } all () { return '1' } } new Numberic().add(1, 1, 1) new Numberic().all(1, 1, 1)
控制檯輸出
npm