[英]calling exports.start in same js file node.js
Hi this is my method in a node js file:您好,这是我在节点 js 文件中的方法:
exports.start = function() {
console.log(' in start of sender.js');
});
How can I call this method in the same js file?如何在同一个js文件中调用这个方法? I tried calling start() and exports.start() but not successful.我尝试调用 start() 和 exports.start() 但没有成功。
Use this code:使用此代码:
var start = exports.start = function() {
console.log(' in start of sender.js');
});
or要么
function start() {
console.log(' in start of sender.js');
});
exports.start = start;
//you can call start();
exports.start = function(){
console.log('testing...')
}
You can call this method like this你可以像这样调用这个方法
exports.start();
You can call the exported function like this您可以像这样调用导出的函数
module.exports.functionName(arguments); module.exports.functionName(参数);
Your named function:您的命名函数:
var start = function() {
console.log(' in start of sender.js');
});
And later export object:然后导出对象:
module.exports = {start : start}
So you can call start()
in the same js file所以你可以在同一个js文件中调用start()
What I'm doing on my computer is the following, and working well - please comment if you think it's a bad idea !我在我的电脑上做的是以下内容,并且运行良好 - 如果您认为这是个坏主意,请发表评论!
Let's say you're on file.js
假设你在file.js
const onThisFile = require("./file");
exports.get = async (args) => .... // whatever;
exports.put = async (args) => .... // whatever;
exports.post = async (args) => .... // whatever;
exports.delete = async (args) => .... // whatever;
exports.doSomething = async (args) => onThisFile.get(args)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.