nodejs定义函数的方法

时间:2024-12-17 10:07:20

test_163:/home/exenode/part3/module_exports # more calc.js
module.exports = {
  sum:function() {
    var result = 0;
    for (var i in arguments) {
      if (!isNaN(arguments[i])){
        result += parseFloat(arguments[i]);
      }
    }
  return result;
  },
};
test_163:/home/exenode/part3/module_exports # more index.js
var calc = require('./calc');

console.log('calc:', calc.sum(1,20,34,-9,56));
test_163:/home/exenode/part3/module_exports #
test_163:/home/exenode/part3/module_exports # node index.js
calc: 102