Module Pattern Continued
by harps116
HTML
<!-- logging outside the console -->
<pre><script>
function log(...args){
document.writeln(args);
document.writeln('<br/>');
}
</script></pre>
JavaScript
// outer function
function hello(name){
//inner private utility function to highlight private functionality
function capitalizeFirstLetter(str) {
return str.split(' ')
.map(w => w[0].toUpperCase() + w.substr(1).toLowerCase())
.join(' ')
}
//using the utility function
function speak(article){
log(`Hi ${capitalizeFirstLetter(article)} ${capitalizeFirstLetter(name)}`);
}
function random(){
return log(`Just a random number between 1 and 10 : ${Math.floor(Math.random() * 11)}`);
}
//this is the public API for the module
var public_api = {
say : speak,
random: random
};
return public_api;
}
var hi = hello('reader');
//calling the method of the returned object.
hi.say('funky functions');
hi.random();