[英]How can I export all functions from a file in JS?
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?使用 ES6
export
,有什么方法可以仅使用一行以默认名称导出文件中的所有函数? For example:例如:
export default all;
The functions are all just in the file, not within an object.这些功能都在文件中,而不是在 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).不,没有通配符导出(除非您从另一个模块重新导出所有内容,但这不是您要问的)。
Simply put export
in front of each function declaration you want exported, eg只需将
export
放在要导出的每个函数声明前面,例如
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 => {
// ...
};
I think there are a lot of solutions to this.我认为对此有很多解决方案。 And as has been answered, there's no wildcard export.
正如已经回答的那样,没有通配符导出。 But, you can 'wildcard' the import.
但是,您可以“通配”导入。 So, I much prefer the one putting
export
before each of the functions you want to expose from the file:因此,我更喜欢将
export
放在要从文件中公开的每个函数之前:
//myfile.js
export function fn1() {...}
export function fn2() {...}
and then import
it like so:然后像这样
import
它:
import * as MyFn from './myfile.js'
Afterwards you could use it like so:之后你可以像这样使用它:
MyFn.fn1();
MyFn.fn2();
You can also use module.exports
as follows:您还可以使用
module.exports
如下:
function myFunction(arg) {
console.debug(arg);
}
function otherFunction(arg) {
console.error(arg);
}
module.exports = {
myFunction: myFunction,
otherFunction: otherFunction,
};
Then you can import it:然后你可以导入它:
import {myFunction, otherFunction} from "./Functions.js";
Regarding my use case, I do have three reusable functions in one file.关于我的用例,我在一个文件中有三个可重用的函数。
utils/reusables.js utils/reusables.js
export const a = () => {}
export const b = () => {}
export const c = () => {}
In order to point the root folder instead of individual file names, I created a file called index.js
which will comprise of all the functions that are listed in individual files.为了指向根文件夹而不是单个文件名,我创建了一个名为
index.js
的文件,它将包含单个文件中列出的所有函数。
utils/index.js实用程序/ index.js
export * from './reusables'
Now, when I want to use my a
function, I will have to simply import it like this现在,当我想使用我的
a
函数时,我将不得不像这样简单地导入它
import { a } from '../utils'
Rather than calling it from its individual files而不是从它的单个文件中调用它
import { a } from '../utils/reusables'
You could also export them at the bottom of your script.您也可以在脚本底部导出它们。
function cube(x) {
return x * x * x;
}
const foo = Math.PI + Math.SQRT2;
var graph = {
options: {
color:'white',
thickness:'2px'
},
draw: function() {
console.log('From graph draw function');
}
}
export { cube, foo, graph };
You can also aggregate submodules together in a parent module so that they are available to import from that module.您还可以在父模块中将子模块聚合在一起,以便它们可以从该模块导入。
// In parentModule.js
export { myFunction, myVariable } from 'childModule1.js';
export { myClass } from 'childModule2.js';
// In top-level module
import { myFunction, myVariable, myClass } from 'parentModule.js'
For Node.js
environment, what I did to export functions was this.对于
Node.js
环境,我所做的导出函数是这样的。
UserController.js用户控制器.js
module.exports = {
signUp: () => {
return "user"
},
login: () => {
return "login"
}
}
UserRouter.js用户路由器.js
const UserController = require('./UserController')
then login
and signUp
functions could be used inside UserRouter
as UserController.signUp()
and UserController.login()
然后可以在
UserRouter
内部使用login
和signUp
函数作为UserController.signUp()
和UserController.login()
I think there's a missing common solution, which is exporting in index.js
file:我认为缺少常见的解决方案,即在
index.js
文件中导出:
myModule/myFunctions.js
export const foo = () => { ... }
export const bar = () => { ... }
then in myModule/index.js
然后在
myModule/index.js
export * from "./myFunctions.js";
This way you can simply import and use it with:通过这种方式,您可以简单地导入并使用它:
import { foo, bar } from "myModule";
foo();
bar();
functions.js函数.js
function alpha(msj) {
console.log('In alpha: ' + msj);
}
function beta(msj) {
console.log('In beta: ' + msj);
}
module.exports = {
alpha,
beta
};
main.js主文件
const functions = require('./functions');
functions.alpha('Hi');
functions.beta('Hello');
Run运行
node main.js
Output输出
In alpha: Hi
In beta: Hello
In case anyone still needs an answer to this in modern JavaScript:如果有人仍然需要现代 JavaScript 中的答案:
const hello = () => "hello there"
const bye = () => "bye bye"
export default { hello, bye }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.