【发布时间】:2017-10-15 17:29:04
【问题描述】:
我正在尝试在节点 JS 中导入简单类,函数执行后我得到 undefined。为什么我得到 undefined?
index.js
const ctrl = require('./math');
console.log(ctrl.addFunction());
console.log(ctrl.sunFunction());
math.js
module.exports = {
addFunction: function() {
console.log('printing addFunction');
},
sunFunction: function() {
console.log('printing sunFunction');
}
};
输出:
节点 index.js
打印 addFunction
未定义
打印太阳功能
未定义
【问题讨论】:
-
undefined是两个函数的返回值,由您的index.js打印。 -
为什么是两个
console.log?只需调用没有console.log即ctrl.addFunction();的函数 -
如果我的函数没有返回任何值,那么它的 undefined 得到了它。谢谢@Sirko
标签: node.js node-modules