Created
June 12, 2020 12:00
-
-
Save oliverwebr/3aec248761a3ba6ebd5a5c4a33e45b9a to your computer and use it in GitHub Desktop.
JS-Closures Example-1: Privat Values
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
const createCounter = () => { | |
let _privatCounter = 0; | |
const increment = () => { | |
_privatCounter += 1; | |
}; | |
const decrement = () => { | |
_privatCounter -= 1; | |
}; | |
const getCounter = () => _privatCounter; | |
return { | |
increment, | |
decrement, | |
getCounter, | |
}; | |
}; | |
const counter = createCounter(); | |
counter.increment(); | |
counter.increment(); | |
console.log(counter.getCounter()); | |
console.log(counter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment