Last active
March 17, 2016 12:44
-
-
Save darklilium/877af389c1dfd91c2bea to your computer and use it in GitHub Desktop.
[JS]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
| /* Es una "stateful function" que mantiene un estado interno pese a que ya se haya terminado de ejecutar la función. | |
| *En js todas las funciones en javascript son closures implicitamente, lo unico que se hizo ahí fue hacerla explicita | |
| *al retornar un objeto que usaba una var interna de la función, en este caso map. Así que mientras el objeto externo | |
| *que tiene los metodos createMap y getMap viva, la variable map seguirá viva independiente de si se terminó de ejecutar la función mymap | |
| */ | |
| Ejemplos: | |
| function mymap(){ | |
| var map; | |
| return { | |
| createMap(div){ | |
| map = new esri.Map(div, { | |
| basemap: "topo", | |
| center:[-71.2905, -33.1009], | |
| zoom:9, | |
| logo: false | |
| }); | |
| return map; | |
| }, | |
| getMap(){ | |
| return map; | |
| } | |
| }; | |
| } | |
| /*******************/ | |
| function token(){ | |
| return { | |
| read(){ | |
| return localStorage.getItem('token'); | |
| }, | |
| write(tokenValue){ | |
| localStorage.setItem('token', tokenValue); | |
| } | |
| }; | |
| } | |
| export default token(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment