Skip to content

Instantly share code, notes, and snippets.

@lamsh
Created August 19, 2016 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lamsh/f16ee7032051868844fc081926bf0854 to your computer and use it in GitHub Desktop.
Save lamsh/f16ee7032051868844fc081926bf0854 to your computer and use it in GitHub Desktop.
Python "if __main__" in Node.js and WSH JScript
/****************************************************************************
\file is_main.js
\author SENOO, Ken
\copyright CC0
*****************************************************************************/
function is_main(this_file_name){
return (typeof(module ) !== "undefined" && !module.parent ) ||
(typeof(WScript) !== "undefined" && WScript.ScriptName === this_file_name)
}
if (typeof(print) === "undefined"){ // for SpiderMonkey
function print(text){
if (typeof(console) !== "undefined") console.log(text );
else if (typeof(WScript) !== "undefined") WScript.Echo(text);
}
}
if (is_main("is_main.js")){
print("Hello");
}
/// \file is_sample_main.js
if (WScript.ScriptName){
WScript.Echo('Failure');
}
if (WScript.ScriptName === "is_sample_main.js"){
WScript.Echo("Not shown");
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- \file is_sample_main.wsf -->
<package>
<job>
<script language="JScript" src="is_sample_main.js"></script>
</job>
</package>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment