[Javascript] Help with closures

Roger Roelofs rer at datacompusa.com
Fri Jan 12 09:33:41 CST 2007


Bill,

On Jan 12, 2007, at 9:02 AM, Bill Moseley wrote:

> Sorry, I wasn't very clear in my question.  I was not about other
> ways to solve the same problem, but about the behavior I'm seeing in
> this example.
>
> What I'm wondering about is, when I have two separate hide/show
> blocks, why are the events from the first block  passing the elements
> of the second block.

I don't have time to actually debug the code, but I can tell you how  
closures and variable scoping work...

The bottom line is: when you refer to a variable inside a closure  
that is set outside the closure, the variable contains its last  
value, not its value when the closure was created.  This usually only  
bites you when you are in a loop context like this.

Here are a couple of links that explain closures in more depth.
http://blog.morrisjohns.com/javascript_closures_for_dummies
http://javascript.crockford.com/

Roger
-- 
Roger Roelofs                          web    www.datacompusa.com
Datacomp Appraisal Services   Email rer at datacompusa.com
3215 Eaglecrest Drive, NE
Grand Rapids, MI  49525-4593





More information about the Javascript mailing list