Skip to content

Instantly share code, notes, and snippets.

@KennethanCeyer
Created March 4, 2017 20:43
Show Gist options
  • Save KennethanCeyer/609879cb173368093316eb7de667f54b to your computer and use it in GitHub Desktop.
Save KennethanCeyer/609879cb173368093316eb7de667f54b to your computer and use it in GitHub Desktop.
Javascript like-module definition.
<script src="math.js"></script>
<script src="main.js"></script>
if (typeof Math !== 'undefined') {
if (typeof Math.sum === 'function') {
console.log(Math.sum(1, 2));
} else {
throw new Error('Math.sum function is not defined.');
}
} else {
throw new Error('A module `Math` is undefined.');
}
var sum = function() {
var total = 0;
for (var idx in arguments) {
total += arguments[idx];
}
return total;
};
window.Math = {
sum: sum
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment