Ad

Call From Module.export ; A Function That Is Inside The Same Module.export

- 1 answer

I need help about this piece of code. What i am trying to do, is to call a function in the "root" of a module.export. But the call provide from a function inside the same module.export. Here is what I got :

From another script, I have the function "base-execute" called. What i'm trying to do, is to call the function "Tocall-Function" just after. Is this possible ?

module.exports.main = {

   Part1 : {
      Sub1 : {
         base-execute(){
           // Some code and then call the "Tocall-Function" //
         }
      },
      Sub2 : {...}
   },
  Part2 : {...}


  Tocall-Function(){
    //Another piece of code//
  }

} 

I tried 'this.Tocall-Function'. But the 'this' itself return only what is written right before the "base-execute" function (what is inside the Sub1 part).

I can access variables that are not inside the module.exports.main. But I can't call it itself.

I can't find something working in my case. If someone can help me out that will be great !

Thank you for reading.

Ad

Answer

You can define main separately from export, and call the function you want to call with main.TocallFunction();

const main = {

  Part1 : {
    Sub1 : {
      baseExecute() {
        main.TocallFunction();
        // Some code and then call the "Tocall-Function" //
      }
    },
    Sub2 : {}
  },
  Part2 : {},
  TocallFunction() {
    //Another piece of code//
  }
};

exports.main = main;
Ad
source: stackoverflow.com
Ad