class Calculate{
add(a,b){
if (typeof a == 'number' && typeof b == 'number') {
return a + b
} else {
return 0
}
}
minus(a,b){
if (typeof a == 'number' && typeof b == 'number') {
return a - b
} else {
return 0
}
}
mul(a,b){
if (typeof a == 'number' && typeof b == 'number') {
return a * b
} else {
return 0
}
}
divide(a,b){
if (typeof a == 'number' && typeof b == 'number') {
return a / b
} else {
return 0
}
}
}
module.exports = Calculate;
测试程序:
let cal = require('cal');
let c = new cal();
console.log(c.add(1,2));
console.log(c.minus(1,2));
console.log(c.mul(1,2));
console.log(c.divide(1,2));
结果: