简体   繁体   English

如何从 JS 文件中导出所有函数?

[英]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内部使用loginsignUp函数作为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.

相关问题 如何从一个索引文件中导出 node.js 中的所有模块、函数等? - How do i export all modules, functions, etc in node.js from one index file? 如何使用单个导出从 React 中的 JS 文件导出所有导入的图像? - How can I export all imported images from a JS file in React using a single export? TypeScript:如何从 JS 文件中导入所有导出函数,然后声明所有这些函数 - TypeScript: How to import all export functions from a JS file and then declare all those functions 如何将 JavaScript 对象的所有功能导出为模块,而无需对文件进行大量更改? - How can I export all functions of a JavaScript object as module without having to make a lot of changes to the file? 如何将 lib.js 文件中的 Discord.js 函数调用到 index.js 中? - How can I call Discord.js functions from a lib.js file into index.js? 如何从文件中导出字符串,并在导出的NODE.js文件中将require()导出到另一个文件中? - How can I export a string from a file and require() the exported string on another file in NODE.js? 如何将两个不同的功能从一个js文件导出到Protractor中的另一个Js文件 - how to export 2 different functions from one js file into another Js file in Protractor 如何在 react naitve 中从一个 index.js 文件中使用 require() 导出图像路径? - How can I export images path with require() from one index.js file in react naitve? 如何将此值导出到另一个 JS 文件中? - How can I export this value into another JS file? 如何从托管在不同环境中的文件执行由 Heroku 托管的 node.js 文件中的函数? - How can I execute functions in a node.js file hosted by Heroku from a file hosted in a different environment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM