m.add不是函数(新节点js模块)

eul*_*ode 2 javascript node.js

我是节点js的新手,并创建模块如下.我得到的是m.add不是Object.anonymous的函数

Module.js

(function(exports, require, module, __filename, __dirname) {
    exports.add = add;
    exports.multiply = multiply;

    function add(number1, number2) {
        return parseInt(number1, 10) + parseInt(number2, 10);
    }

    function multiply(number1, number2) {
        return parseInt(number1, 10) * parseInt(number2, 10);
    }
});
Run Code Online (Sandbox Code Playgroud)

App.js

var m = require('./module');

console.log(m.add(3, 5));
console.log(m.multiply(4, 5));
Run Code Online (Sandbox Code Playgroud)

And*_*ndy 6

不确定为什么要包装这样的代码,但这里module.js应该看看:

function add(number1, number2) {
  return parseInt(number1, 10) + parseInt(number2, 10);
}

function multiply(number1, number2) {
  return parseInt(number1, 10) * parseInt(number2, 10);
}

// don't export your functions individually
// export them on an object (which is how you use them in your app)
module.exports = {
  add: add,
  multiply: multiply
}
Run Code Online (Sandbox Code Playgroud)

如果你愿意,你可以同时为你的函数执行各项出口(如你有你的 module.js),但它并不像你需要太多.