0

I have this snippet of code:

2-iife.js

    const counter = (function() {
            let count = 0
    
            return {
                    inc: function() { count = count + 1 },
                    get: function() { console.log(count) },
            }
    })()
    
    counter.get()
    counter.inc()
    counter.get()

And it prints out:

0

1

I expect an object like this:

{
    inc: function() { count = count + 1 },
    get: function() { console.log(count) },
}

to be assigned to the counter constant variable.

Then how the heck does the counter.get() know what count even is to print out or increment it as I invoke counter.get() and counter.inc()?

Dat Pham
  • 31
  • 7

0 Answers0