Last active
September 18, 2016 18:01
-
-
Save nash403/eeab316f7377aa4e36b1a0966a525841 to your computer and use it in GitHub Desktop.
Writing JS for both Node & Browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="mymodule.js"></script> | |
<script> | |
alert(mymodule.test()); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
// your code goes here | |
function notExported(){ | |
return "shh, I'm not here"; | |
} | |
function test(){ | |
return 'hello world'; | |
}; | |
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { | |
module.exports = test; // default | |
// module.exports.otherExport = otherExport; | |
} | |
else { | |
window.test = test; | |
// module.exports.otherExport = otherExport; | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mymodule = require('./mymodule'), | |
sys = require('sys'); | |
sys.puts(mymodule.test()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment