我是节点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); } });
App.js
var m = require('./module'); console.log(m.add(3, 5)); console.log(m.multiply(4, 5));
Andy.. 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 }
如果你愿意,你可以同时为你的函数执行各项出口(如你有你的 module.js
),但它并不像你需要太多.
不确定为什么要包装这样的代码,但这里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 }
如果你愿意,你可以同时为你的函数执行各项出口(如你有你的 module.js
),但它并不像你需要太多.