如何从 JS 中的文件中导出所有函数? [英] How can I export all functions from a file in JS?

查看:66
本文介绍了如何从 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆