From d92571e6be3d0bbf127006296128f3d730708941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=96=E8=99=B9=E9=9C=96?= <1070362934@qq.com> Date: Thu, 24 Feb 2022 23:41:40 +0800 Subject: [PATCH] 20220224 --- .../demo01.js" | 32 +++++++++++++++++++ .../demo02.js" | 15 +++++++++ 2 files changed, 47 insertions(+) create mode 100644 "\350\265\226\350\231\271\351\234\226/20220224\345\233\233\345\210\231\350\277\220\347\256\227/demo01.js" create mode 100644 "\350\265\226\350\231\271\351\234\226/20220224\345\233\233\345\210\231\350\277\220\347\256\227/demo02.js" 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 0000000..1d6014a --- /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 0000000..5939f5c --- /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 -- Gitee