依赖:
.babelrcc
文件
{
"presets": ["@babel/preset-env"],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose" : true }]
]
}
index.js
文件
function validate(target, key, descriptor) {
const func = descriptor.value;
descriptor.value = function (...args) {
for (const num of args) {
if('number' !== typeof num) {
throw new Error(`"${num}" is not number`);
}
}
return func.apply(this, args);
}
}
class NumBeric {
PI = 3.1415926
@validate
add(...nums){
return nums.reduce((p, c) => (p + c), 0)
}
}
console.log(new NumBeric().add(1, 'x'))