Javascript module.exports 在一行中包含所有功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33589571/
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 15:15:19  来源:igfitidea点击:

module.exports that include all functions in a single line

javascriptnode.jsimportheader

提问by user781486

This is a follow-up question to In Node.js, how do I "include" functions from my other files?

这是在 Node.js 中的后续问题,我如何“包含”其他文件中的函数?

I would like to include an external js file that contains common functions for a node.js app.

我想包含一个外部 js 文件,其中包含 node.js 应用程序的常用功能。

From one of the answers in In Node.js, how do I "include" functions from my other files?, this can be done by

在 Node.js中的答案之一,我如何“包含”其他文件中的函数?,这可以通过

// tools.js
// ========
module.exports = {
  foo: function () {
    // whatever
  },
  bar: function () {
    // whatever
  }
};

var zemba = function () {
}

It is inconvenient to export each and every function. Is it possible to have a one-liner that exports all functions? Something that looks like this;

不方便导出每一个函数。是否有可能有一个导出所有功能的单线?看起来像这样的东西;

module.exports = 'all functions';

It is so much more convenient this way. It is also less buggy in case one forgets to export certain functions later.

这样就方便多了。如果稍后忘记导出某些功能,它也不会出错。

If not a one-liner, are there simpler alternatives that make coding more convenient? I just want to include an external js file made up of common functions conveniently. Something like include <stdio.h>in C/C++.

如果不是单线,是否有更简单的替代方案使编码更方便?我只想方便地包含一个由常用函数组成的外部js文件。类似于include <stdio.h>C/C++ 中的东西。

回答by Timur Ridjanovic

You can write all your function declarations first and then export them in an object:

您可以先编写所有函数声明,然后将它们导出到一个对象中:

function bar() {
   //bar
}

function foo() {
   //foo
}

module.exports = {
    foo: foo,
    bar: bar
};

There's no magical one-liner though, you need to explicitly export the functions you want to be public.

虽然没有神奇的单线,但您需要明确导出您想要公开的功能。

回答by Peter Huffer

I have done something like the following:

我做了类似以下的事情:

var Exported = {
   someFunction: function() { },
   anotherFunction: function() { },
}

module.exports = Exported;

I require it in another file and I can access those functions

我在另一个文件中需要它,我可以访问这些功能

var Export = require('path/to/Exported');
Export.someFunction();

This is essentially just an object with functions in it, and then you export the object.

这本质上只是一个带有函数的对象,然后您导出该对象。

回答by EpicDavi

It is worth noting that in ES6, you can now export functions like this:

值得注意的是,在 ES6 中,您现在可以导出如下函数:

export function foo(){}
export function bar(){}
function zemba(){}

Simply write exportbefore the functions you want to export. More information here.

只需export在要导出的函数之前写上即可。更多信息在这里

回答by Weslei Prudencio

If you use ES6 you can do something like that:

如果您使用 ES6,您可以执行以下操作:

function bar() {
   //bar
}

function foo() {
   //foo
}

export default { bar, foo };

回答by Joe Bourne

A really old question but I just had to solve the same issue myself. the solution I used was to define a Class inside the module to contain all my functions and simply export an instance of the class.

一个非常老的问题,但我只需要自己解决同样的问题。我使用的解决方案是在模块内定义一个 Class 来包含我的所有函数,并简单地导出该类的一个实例。

classes.js looks like this:

classes.js 看起来像这样:

class TestClass
{
   Function1() {
        return "Function1";
    } 
    Function2() {
        return "Function2";
    }
}

module.exports = new TestClass();

app.js looks like this:

app.js 看起来像这样:

const TestClass = require("./classes");
console.log( TestClass.Function1);

just keep adding more functions to the class and they will be exported :)

只需继续向类添加更多函数,它们就会被导出:)