【发布时间】:2015-11-30 10:26:26
【问题描述】:
我在nodejs中写了一个模块,里面Test.js,代码爆了
function Test() {
this.key = 'value';
}
Test.prototype.foo = function () { return 'foo'; }
module.exports = Test;
然后,在 B.js 中
var Test = require('./services/Test');
var test = new Test();
console.log(test.foo());
不幸的是,我得到了“未定义的方法 foo”, 谁能告诉我发生了什么?非常感谢
【问题讨论】:
-
在 B.JS 中 var test = require('./services/Test'); console.log(test.foo());
-
可能你的 JS 文件不在正确的位置。当我复制您的代码时,它可以工作。你可以在一个干净的 NodeJS 应用程序中尝试它,它会工作的。也尝试调试它。并让 IDE 自动完成您的测试文件路径。
-
我认为路径是正确的,它可以正确的“require”,但错误的“new”
-
console.log(Test, test, test.key)记录什么?
标签: javascript node.js module commonjs