Created
March 24, 2016 11:46
-
-
Save aalvesjr/7a5b478590d792863f3c to your computer and use it in GitHub Desktop.
JavaScript: Closures
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
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