这是我正在导出的 app.js
中的函数:
var app = {
start: function() {
//sample function
exports.msgReceived = function() {
return "Tomorrow 8pm near city hall";
}
}
}
module.exports = app;
我有一个名为 messages.js
的文件,我想在其中调用 start
函数和 msgReceived
函数:
let subscriber = require('app');
it('server gets connected', function(done) {
subscriber.start(); // working
subscriber.msgReceived(); // not working
done();
});
我怎样才能做到这一点?
因为使用 var
关键字我只能访问 start
函数。我的代码编辑器 IntelliSense 未指向 msgReceived
函数。
最佳答案
您可以执行类似的操作,在运行 start()
后在应用程序对象上设置新函数。
var app = {
start: function() {
//sample function
this.msgReceived = function() { // `this` refers to `app`
return "Tomorrow 8pm near city hall";
}
}
}
module.exports = app;
如果你想让智能感知工作,你可能必须包括
var app = {
start : ...,
msgReceived: null // or undefined... or anything.
}
关于javascript - 如何在 NodeJS 中调用导出函数?哪些可以嵌套?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52039353/