const crateCounter = () => {
let count = 0
const increment = () => {
count++
}
const getCount = () => {
return count
}
return { increment, getCount, count }
}
let counter = crateCounter()
counter.increment() // count is 1
counter.increment() // count is 2
counter.increment() // count is 3
console.log(counter.getCount()) // showing 3. Good!
console.log(counter.count) // showing 0. Why?
Why the method getCount()
get access to the variable count
but why we cannot access to the variable count
itself?