How to pass more than one function in a .js file to another file


#1

Hello

I am trying to pass another function in a .js file to html. I used “module.exports” but it will only pass one function. Should I set all functions in one main function or something similar. If so please provide details. If there is an easier way please provide the detail to that.


#2

module.exports = {
fn1: _fn1,
fn2: _fn2
};

or with es6:

module.exports = { fn1, fn2 };


#3

Sorry for the late response

How would I set this up? I am still very new to electron. I tried it by module .exports examples provided. I know i am doing this the wrong way but I’ll learn it by example provided. I have to call the functions in the index.html file. how do I go about setting it up. I did the “var name = require(’ ');”. The problem is it only calls one and not both. I should mention each function opens its own file to be read in and stored in an array.

here is my set up I am stuck on how to structure things and call them in the html file.

functionList(callback){
    function1(callback){
    code.....
    }
    callback()

    function2(callback){
    code....
    }
   callback()
}

 module.exports = functionList

#4

This isn’t really an electron problem, just so ya know. Maybe look into revealing module pattern or prototype pattern for javascript. You should really get familiar with those, this stuff isn’t electron specific.

   var blah = (function() {
     function _first () {
       console.log('first!');
     }
     function _second () {
       console.log('second!');
     }

     return {
       first: _first,
       second: _second
     };
   })();

   module.exports = blah


// in other file
var blah = require('./path/to/blah);
blah.first(); // prints 'first!'

#5

I know it is not per say…but I was stuck. Thanks for the input