/RevealingModulePattern.js Secret
Last active
April 16, 2019 16:01
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 myGradesCalculate = (function () { | |
// Keep this variable private inside this closure scope | |
var myGrades = [93, 95, 88, 0, 55, 91]; | |
var average = function() { | |
var total = myGrades.reduce(function(accumulator, item) { | |
return accumulator + item; | |
}, 0); | |
return'Your average grade is ' + total / myGrades.length + '.'; | |
}; | |
var failing = function() { | |
var failingGrades = myGrades.filter(function(item) { | |
return item < 70; | |
}); | |
return 'You failed ' + failingGrades.length + ' times.'; | |
}; | |
// Explicitly reveal public pointers to the private functions | |
// that we want to reveal publicly | |
return { | |
average: average, | |
failing: failing | |
} | |
})(); | |
myGradesCalculate.failing(); // 'You failed 2 times.' | |
myGradesCalculate.average(); // 'Your average grade is 70.33333333333333.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment