在一行中包含所有功能的module.exports [英] module.exports that include all functions in a single line
问题描述
我想包含一个外部js文件,其中包含一个node.js应用程序的常用功能.
I would like to include an external js file that contains common functions for a node.js app.
From one of the answers in In Node.js, how do I "include" functions from my other files?, this can be done by
// tools.js
// ========
module.exports = {
foo: function () {
// whatever
},
bar: function () {
// whatever
}
};
var zemba = function () {
}
导出每个功能很不方便.是否可以单线导出所有功能?看起来像这样;
It is inconvenient to export each and every function. Is it possible to have a one-liner that exports all functions? Something that looks like this;
module.exports = 'all functions';
这种方式非常方便.如果以后忘记导出某些功能,它的错误也较少.
It is so much more convenient this way. It is also less buggy in case one forgets to export certain functions later.
如果不是单线的,是否有更简单的替代方法使编码更方便?我只想方便地包含一个由通用功能组成的外部js文件.类似于C/C ++中的include <stdio.h>
.
If not a one-liner, are there simpler alternatives that make coding more convenient? I just want to include an external js file made up of common functions conveniently. Something like include <stdio.h>
in C/C++.
推荐答案
您可以先编写所有函数声明,然后将它们导出到对象中:
You can write all your function declarations first and then export them in an object:
function bar() {
//bar
}
function foo() {
//foo
}
module.exports = {
foo: foo,
bar: bar
};
虽然没有神奇的一线工具,但您需要显式导出要公开的功能.
There's no magical one-liner though, you need to explicitly export the functions you want to be public.
这篇关于在一行中包含所有功能的module.exports的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!