Post details: Closures in JavaScript
2008-01-09
Closures in JavaScript
There are cases when you need to use JavaScript closures and there are cases when you accidentally create closures when you don’t need to. Here are some articles to help understand them:
“JavaScript Closures for Dummies” [blog.morrisjohns.com]
“JavaScript Closures” [jibbering.com]
“AJAX in Action: Appendix B - JavaScript for Object-Oriented Programmers” [java.sun.com]
Interesting & related:
“Closures and executing JavaScript on page load” - Simon Willison on chaining multiple onLoad events
“Private members in JavaScript” - Douglas Crockford on implementing private class members
TrimBreakpoint - a tricky use of closures to let you inspect local variables for a function from a popup breakpoint window.
A JavaScript closure preserves references to all the local variables after the function has finished.
If a function is defined within another function, a closure is created. Then the inside function (e.g. when called later, using a reference to it) can still access and modify variables of the outside function, even after the outside function has quit.
Note: this explanation is a very simplified view and you may want to consult articles referenced here for more detailed information.
Comments, Pingbacks:
No Comments/Pingbacks for this post yet...
This post has 21 feedbacks awaiting moderation...
Leave a comment:
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|
| << < | > >> | |||||
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 | |||
Search
Gallery
www.flickr.com
|
Categories
Archives
- February 2011 (1)
- September 2010 (1)
- October 2009 (1)
- March 2009 (2)
- February 2009 (4)
- January 2009 (2)
- December 2008 (2)
- November 2008 (5)
- October 2008 (10)
- August 2008 (1)
- July 2008 (4)
- June 2008 (1)
- More...
- more...

