Created
March 4, 2017 20:43
-
-
Save KennethanCeyer/609879cb173368093316eb7de667f54b to your computer and use it in GitHub Desktop.
Javascript like-module definition.
This file contains hidden or 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="math.js"></script> | |
<script src="main.js"></script> |
This file contains hidden or 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
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.'); | |
} |
This file contains hidden or 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 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