Last active
May 7, 2020 10:50
-
-
Save lorenzofelletti/be79f566ec6442d025685e4eb128b220 to your computer and use it in GitHub Desktop.
A version of the other gist closure.js but with a simpler syntax for beginners
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 makeCounter(initialValue) { | |
let count = initialValue; | |
function toString() { | |
return 'The count is ' + count +'.'; | |
} | |
return { | |
add: function() { return ++count }, | |
sub: function() { return --count }, | |
val: function() { return count }, | |
toString: toString | |
}; | |
} | |
var counter = makeCounter(0); | |
console.log(counter.add()) // 1 | |
console.log(counter.val()) // 1 | |
console.log(counter.sub()) // 0 | |
console.log(counter.add()) // 1 | |
console.log(counter.add()) // 2 | |
console.log(counter.toString()) // The count is 2. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment