diff --git "a/\350\265\226\350\231\271\351\234\226/20220224\345\233\233\345\210\231\350\277\220\347\256\227/demo01.js" "b/\350\265\226\350\231\271\351\234\226/20220224\345\233\233\345\210\231\350\277\220\347\256\227/demo01.js" new file mode 100644 index 0000000000000000000000000000000000000000..1d6014a7c2fd7c29da3ce658e107e2415f095e9c --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220224\345\233\233\345\210\231\350\277\220\347\256\227/demo01.js" @@ -0,0 +1,32 @@ +// 声明一个对象有加减乘除的属性 +// 暴露一个函数 函数传递运算的选择 + +// 没改的 直接暴露对象 +// module.exports.arithmetic = arithmetic; + +var arithmetic = { + + '+': function (a, b) { + return a + b + }, + '-': function (a, b) { + return a - b + }, + '*': function (a, b) { + return a * b + + }, + '/': function (a, b) { + return a / b + }, +} + function handle(a,b,symbol){ + if(isNaN(a) || isNaN(b)){ + throw Error('请输入正确数值'); + }if (a===0 || b===0 && symbol==='/') { + throw Error('0不能作为除数或被除数'); + } + return arithmetic[symbol](a,b); + } + module.exports.handle = handle; + module.exports.arithmetic = arithmetic; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220224\345\233\233\345\210\231\350\277\220\347\256\227/demo02.js" "b/\350\265\226\350\231\271\351\234\226/20220224\345\233\233\345\210\231\350\277\220\347\256\227/demo02.js" new file mode 100644 index 0000000000000000000000000000000000000000..5939f5c51e447d448ef42bb46c1e666e094c4b5e --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220224\345\233\233\345\210\231\350\277\220\347\256\227/demo02.js" @@ -0,0 +1,15 @@ +var calculator = require('./demo01'); +try{ + console.log(calculator.handle(1,2,'+')); + console.log(calculator.handle(3,4,'-')); + console.log(calculator.handle(5,6,'/')); + console.log(calculator.handle(7,8,'*')); + // 没改的直接暴露对象 + console.log("---------------------------------") + console.log(calculator.arithmetic["+"](3,5)); + console.log(calculator.arithmetic["-"](4,5)); + console.log(calculator.arithmetic["*"](5,5)); + console.log(calculator.arithmetic["/"](6,5)); +}catch(error){ + console.log(error.message); +} \ No newline at end of file