1

I write a module with some functions, and execute the functions in the module.

My object is:

If I run the module alone, those functions can be executed. But if it is imported by another function as a module, then those functions should not be executed.

Like the code as below for demonstration:

In the module, for example, moduleFunction.js:

function moduleFunction(){
  // some code run here
}
moduleFunction();  // run the function in standalone script, but not when import the module.
export default moduleFunction;

In another module, for example runModule.js:

import moduleFunction from "moduleFunction";
moduleFunction();

I already know I can achieve the task in Python by if __name__ == "__main__": . Is there a way to get it done in JavaScript?

soarinblue
  • 1,517
  • 3
  • 21
  • 30
  • 2
    Looks like you can't: http://stackoverflow.com/questions/34842738/if-name-main-equivalent-in-javascript-es6-modules – kalanchloe Feb 10 '17 at 04:38

0 Answers0