decrator
function log(target) {
console.log(Object.getOwnPropertyDescriptor(target.prototype)); // undefined 这里打印 ??????
const desc = Object.getOwnPropertyDescriptor(target.prototype);
for (const key of Object.keys(desc)) {
if (key === 'constructor') {
continue;
}
const func = desc[key].value;
if ('function' === typeof func) {
Object.defineProperty(target.prototype, key, {
value(...args) {
console.log('before', key);
const ret = func.apply(this, args);
console.log('after', key);
return ret;
}
})
}
}
}
@log
class Numberic {
PI = 3.1415926;
add (...nums) {
return nums.reduce((p, n) => ( p + n ), 0)
}
}
new Numberic(1, 2);