Skip to content

Instantly share code, notes, and snippets.

@aalvesjr
Created March 24, 2016 11:46
Show Gist options
  • Save aalvesjr/7a5b478590d792863f3c to your computer and use it in GitHub Desktop.
Save aalvesjr/7a5b478590d792863f3c to your computer and use it in GitHub Desktop.
JavaScript: Closures
function initSeq() {
var i=0;
return function(){
i += 1;
return i
}
}
var a = initSeq()
, b = initSeq()
;
a()
//=> 1
a()
//=> 2
a()
//=> 3
b()
//=> 1
a()
//=> 4
b()
//=> 2
// based on https://gobyexample.com/closures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment