nodejs定义函数的方法

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

posted @ 2016-05-13 10:29  zhoudingzhao  阅读(4725)  评论(0编辑  收藏  举报