So i was watching this video and at 18:14 he has an example showcasing closures in the programming language "Groovy". https://www.youtube.com/watch?v=7aYS9PcAITQ
Can JavaScript "save" instances in the same way using closures and if so, could somebody wrap up a little codesample for me to study? I've been reading up on closures in JavaScript and essentially i know that if you have a function within a function and the outer function returns the inner function can be kept alive and that's part of what a closure is.
def makeCounter() {
def very_local_variable = 0
return { very_local_variable += 1 }
}
c1 = makeCounter()
c1()
c1()
c1()
c2 = makeCounter()
println "C1 = ${c1()}, C2 = ${c2()}"
OUTPUT and closures:
C1 = 4, C2 = 1