如何从JS中的文件导出所有功能? [英] How can I export all functions from a file in JS?
问题描述
我正在创建一个单位转换器,并且我想将所有转换函数放入它们自己的文件中.使用ES6 export
,是否有任何方法可以仅使用一行来导出文件中具有其默认名称的所有功能?例如:
I'm creating a unit converter, and I want to put all of the conversion functions into their own file. Using ES6 export
, is there any way to export all of the functions in the file with their default names using only one line? For example:
export default all;
所有功能仅在文件中,而不在对象中.
The functions are all just in the file, not within an object.
推荐答案
不,没有通配符导出(除非您从另一个模块中重新导出所有内容,但这不是您所需要的)重新询问).
No, there's no wildcard export (except when you're re-exporting everything from another module, but that's not what you're asking about).
只需将export
放在要导出的每个函数声明的前面,例如
Simply put export
in front of each function declaration you want exported, e.g.
export function foo() {
// ...
}
export function bar() {
// ...
}
...或者,当然,如果您使用的是函数表达式:
...or of course, if you're using function expressions:
export var foo = function() {
// ...
};
export let bar = () => {
// ...
};
export const baz = value => {
// ...
};
这篇关于如何从JS中的文件导出所有功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!