Module.exports that include all functions in one line

This is the next question In Node.js, how do I enable “include” functions from my other files?

I would like to include an external js file containing common functions for the Node.js application.

From one of the answers in Node.js, how do I enable the “include” functions from my other files? this can be done with

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

var zemba = function () {
}

Inconvenient to export each function. Is it possible to have a one-line mechanism that exports all functions? Something like this:

module.exports = 'all functions';

It is much more convenient. It is also less buggy if you later cannot export certain functions.

, , ? js , . - include <stdio.h> C/++.

+11
5

, :

function bar() {
   //bar
}

function foo() {
   //foo
}

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

, , .

+25

, ES6 :

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

export , . .

+8

- :

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

module.exports = Exported;

,

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

, .

+7

ES6, - :

function bar() {
   //bar
}

function foo() {
   //foo
}

export default { bar, foo };
Hide result
+3

, . , , , , , .

classes.js :

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

module.exports = new TestClass();

app.js :

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

, :)

+1

Source: https://habr.com/ru/post/1614902/


All Articles