【问题标题】:How can I export all functions from a file in JS?如何从 JS 文件中导出所有函数?
【发布时间】:2018-09-11 23:41:07
【问题描述】:

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

export default all;

函数都在文件中,而不是在对象中。

【问题讨论】:

  • @Hamms:除了 OP 没有对象,所以...
  • 我会考虑导出一个包含所有函数的对象:export default { fn1(), fn2(), ... },或者将它们包装在 class 然后 export default MyClass 中。另一种解决方案是将export放在每个函数前面,import * as MyConverter from './myconverter.js'

标签: javascript ecmascript-6


【解决方案1】:

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

只需将export 放在要导出的每个函数声明的前面,例如

export function foo() {
    // ...
}
export function bar() {
    // ...
}

...当然,如果您使用的是函数表达式:

export var foo = function() {
    // ...
};
export let bar = () => {
    // ...
};
export const baz = value => {
    // ...
};

【讨论】:

  • 我看不出有任何理由将赋值与函数表达式一起使用。 export function 声明要简单得多,甚至更短。
  • @Bergi:我同意(除了箭头函数是轻量级的可能参数),只是想确保 OP 知道在哪里以及如何将export 应用于他们正在做的事情。
【解决方案2】:

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

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

然后import 就像这样:

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

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

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

【讨论】:

  • 首先我觉得很好,然后我也可以导出和导入我的“全局”变量。不幸的是,导入的东西是只读的。我不知道为什么,但stackoverflow.com/a/58402982/1707015 找到了补救措施。
  • 这在我的情况下有效,但我无法在我的 packege.json 中添加“type”:“module”的情况下运行该文件。
  • @moghwan 请注意,当您使用纯 NodeJS 时,您需要在 package.json 中使用 "type": "module"
【解决方案3】:

您也可以使用module.exports,如下:

function myFunction(arg) {
  console.debug(arg);
}
function otherFunction(arg) {
  console.error(arg);
}

module.exports = {
  myFunction: myFunction,
  otherFunction: otherFunction,
};

然后就可以导入了:

import {myFunction, otherFunction} from "./Functions.js";

【讨论】:

  • 你不需要把名字写两次。只是 module.exports ={myFunction,otherFunction} 也一样。
  • 是的,这是真的,我只是展示了你可以编写它来将“myrandofunc”也导出为“thisisabettername_forthefunction”,尽管我不明白你为什么不只是重命名函数! lmao @me - 有时我喜欢迂腐?但是,是的,谢谢!
【解决方案4】:

关于我的用例,我确实在一个文件中有三个可重用的函数。

utils/reusables.js

export const a = () => {}
export const b = () => {}
export const c = () => {}

为了指向根文件夹而不是单个文件名,我创建了一个名为 index.js 的文件,该文件将包含单个文件中列出的所有函数。

utils/index.js

export * from './reusables'

现在,当我想使用我的 a 函数时,我将不得不像这样简单地导入它

import { a } from '../utils'

而不是从单个文件中调用它

import { a } from '../utils/reusables'

【讨论】:

  • 你确定这个export a = () => {} 有效吗?我得到Declaration or statement expected.
  • @Qwerty 好像我忘了添加 const 关键字。让我更新我的答案。但是,这应该有效。 export const a = () => {}
【解决方案5】:

您也可以在脚本底部导出它们。

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 };

您还可以在父模块中将子模块聚合在一起,以便可以从该模块导入。

// 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'

【讨论】:

    【解决方案6】:

    对于Node.js环境,我做的导出函数是这样的。

    UserController.js

    module.exports = {
      signUp: () => {
        return "user"
      },
      login: () => {
        return "login"
      }
    }
    

    UserRouter.js

    const UserController = require('./UserController')
    

    那么loginsignUp 函数可以在UserRouter 中作为UserController.signUp()UserController.login() 使用

    【讨论】:

    • 实际上,我们如何在 import 而不是 require 的情况下使用该解决方法?
    • v12 以下的节点不支持 es6 导入/导出。但是,您将能够在其他地方使用它。见stackoverflow.com/questions/34278474/…
    • 或者,类似exports.signUp = () => "user" 的东西也可以工作
    【解决方案7】:

    我认为缺少一个通用解决方案,即在index.js 文件中导出:

    myModule/myFunctions.js

    export const foo = () => { ... }
    export const bar = () => { ... }
    

    然后在myModule/index.js

    export * from "./myFunctions.js";
    

    这样您就可以简单地导入并使用它:

    import { foo, bar } from "myModule";
    foo();
    bar();
    

    【讨论】:

      【解决方案8】:

      functions.js

      function alpha(msj) {
          console.log('In alpha: ' + msj);
      }
      
      function beta(msj) {
          console.log('In beta: ' + msj);
      }
      
      module.exports = {
          alpha,
          beta
      };
      

      ma​​in.js

      const functions = require('./functions');
      functions.alpha('Hi');
      functions.beta('Hello');
      

      运行

      node main.js
      

      输出

      In alpha: Hi
      In beta: Hello
      

      【讨论】:

        猜你喜欢
        • 2021-04-14
        • 2019-12-13
        • 2020-12-31
        • 2017-01-04
        • 2020-09-20
        • 2021-11-19
        • 1970-01-01
        • 2020-12-12
        • 1970-01-01
        相关资源
        最近更新 更多