admin管理员组

文章数量:1332403

quick question.

I know how to export/import functions if they are placed like so

module.exports = {
    get: get,
    set: set
};

but i do not know how to run this function from another file, what exacly do i need to import/export?

 module.exports = function() {  
 var this = {};
 var that = {}; .... 
 much more code
 ....

quick question.

I know how to export/import functions if they are placed like so

module.exports = {
    get: get,
    set: set
};

but i do not know how to run this function from another file, what exacly do i need to import/export?

 module.exports = function() {  
 var this = {};
 var that = {}; .... 
 much more code
 ....
Share Improve this question edited Mar 28, 2018 at 9:16 rustyx 85.6k27 gold badges222 silver badges292 bronze badges asked Mar 28, 2018 at 9:15 joe123joe123 571 gold badge1 silver badge7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Suppose you have a two files A.js and B.js

A.js

module.exports = function() {  
   var this = {};
   var that = {}; .... 
   much more code
   ....
}

Now if you want to use this in B.js then A.js is using default exports and it is exporting a function so you can use it like this.

var a = require('./A.js');
// now as A.js is exporing a function so you can call that function by invoking a() function 
// as you have inported it into variable name a
a(); // this will call that

If your function needs arguments like this module.exports = function(x, y) {

then you need can pass it like

a(1, 2);

I do not know, what you necessarily mean when you say "knowing how to import/export functions" but here is what you could do to define a function and later reuse it from another file.

test.js

module.exports = () => {
   console.log('This is a sample function')
}

use.js

const myfunc = require('./test');
myfunc(); // Would print This is a sample function

I have assumed test.js and use.js to be in the same directory.

You could also have multiple functions in a file: test.js

module.exports.fn1 = () => {
   console.log('This is sample function1')

}

module.exports.fn2 = () => {
   console.log('This is sample function2')
}

use.js

const myfunc1 = require('./test').fn1;
myfunc1(); 
console.log(require('./test').fn2); // Directly if you want

You can also read about:

  • import statement which currently is not supported by NodeJs but has a way with babel.

You have two ways to export the function of a module (archive js):

1) "By default" -> If you have only to export one function or other data in the same archive. In that case, you can import with the alias you want:

export default myFunction() {...}
(In the other archive)
import alias you want(the same name or other) from `'./name_of_the_archive_to_import';

2) "Several functions or objects" -> If you have to export several functions or other data in the same archive. In that case, you have to call(import) those variables with the same name that were declared:

export variable1;
export variable2;
...
(In the other archive)
import variable1 from './name_of_the_archive_to_import';
import variable2 from './name_of_the_archive_to_import';
...

本文标签: javascriptmoduleexportsfunction() how to callStack Overflow