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?