| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c395012 commit 430d4d3
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,10 +20,15 @@ | |||
| 20 | 20 | var my = setup(); // alerts 1 | |
| 21 | 21 | my(); // alerts 2 | |
| 22 | 22 | ||
| 23 | - var setup = function (count) { | ||
| 23 | + | ||
| 24 | + | ||
| 25 | + // Your setup function can store some private data in a closure and use that data somehow. | ||
| 26 | + // Here setup() creates a counter function, which gives a next ID for example. But the count variable is not exposed. | ||
| 27 | + | ||
| 28 | + var setup = function () { | ||
| 24 | 29 | var count = 0; | |
| 25 | 30 | return function () { | |
| 26 | - return (count += 1); | ||
| 31 | + return ++count; | ||
| 27 | 32 | }; | |
| 28 | 33 | }; | |
| 29 | 34 | // usage | |
@@ -33,8 +38,7 @@ | |||
| 33 | 38 | //next(); // returns 3 | |
| 34 | 39 | ||
| 35 | 40 | // reference | |
| 36 | - // http://www.jspatterns.com/ | ||
| 37 | - // http://shop.oreilly.com/product/9780596806767.do?sortby=publicationDate | ||
| 41 | + // http://www.jspatterns.com/returning-functions/ | ||
| 38 | 42 | </script> | |
| 39 | 43 | </body> | |
| 40 | 44 | </html> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments