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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49616639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:25:17  来源:igfitidea点击:

How can I export all functions from a file in JS?

javascriptecmascript-6

提问by JakAttk123

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:

我正在创建一个单位转换器,并且我想将所有转换函数放入它们自己的文件中。使用 ES6 export,有什么方法可以仅使用一行以默认名称导出文件中的所有函数?例如:

export default all;

export default all;

The functions are all just in the file, not within an object.

这些函数都只是在文件中,而不是在一个对象中。

回答by T.J. Crowder

No, there's no wildcard export (except when you're re-exportingeverything from another module, but that's not what you're asking about).

不,没有通配符导出(除非您从另一个模块重新导出所有内容,但这不是您要问的)。

Simply put exportin front of each function declaration you want exported, e.g.

只需将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 => {
    // ...
};

回答by ionizer

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 exportbefore each of the functions you want to expose from the file:

我认为对此有很多解决方案。正如已经回答的那样,没有通配符导出。但是,您可以“通配”导入。因此,我更喜欢在export要从文件中公开的每个函数之前放置一个:

//myfile.js
export function fn1() {...} 
export function fn2() {...}

and then importit like so:

然后import它像这样:

import * as MyFn from './myfile.js'

Afterwards you could use it like so:

之后你可以像这样使用它:

MyFn.fn1();
MyFn.fn2();

回答by scot

You can also use module.exportsas 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";

回答by Pavindu

For Node.jsenvironment, 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 loginand signUpfunctions could be used inside UserRouteras UserController.signUp()and UserController.login()

thenloginsignUp函数可以在内部UserRouter用作UserController.signUp()UserController.login()

回答by CloudBranch

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'