Created
July 17, 2012 17:40
-
-
Save renatoargh/3130758 to your computer and use it in GitHub Desktop.
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 criadorDeClosure(param){ | |
| var aux = 0; | |
| return function(){ | |
| aux++; | |
| console.log("aux: %s - param: %s", aux, param); | |
| }; | |
| } | |
| var closure1 = criadorDeClosure(1); | |
| closure1(); | |
| closure1(); | |
| //Perceba o incremento de aux | |
| var closure2 = criadorDeClosure(2); | |
| closure2(); | |
| //aux == 1, pois a referencia de aux neste caso não | |
| //é a mesma que a referencia de aux em closure1 | |
| //Este código loga: | |
| // aux: 1 - param: 1 # da chamada de closure1(); | |
| // aux: 2 - param: 1 # da segunda chamada de closure1(); | |
| // aux: 1 - param: 2 # da chamada de closure2(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment