[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Forward declarations in modules
- From: "Gavin Kistner" <gavin.kistner@...>
- Date: Thu, 7 Sep 2006 15:59:35 -0600
> > a = 30 -- no local here
> > print( a ) --> 30
> > f( ) --> 10
> > g( ) --> 30
>
> The surprising part here is that the last one prints 30 and not 20.
> However it is consistent with my forward declaration problem.
>
> The last "a = 30" must have modified the *most recently declared*
> local variable, hence f prints 10 but g prints 30.
That's the internal model I'm seeing. Closures do not bind to values,
but rather to variables that hold or reference values. Each usage of
'local' creates a new variable.
"a = 30" has to figure out what 'a' is, and finds the recently-declared
'a' which the 'g' closure is also using. Modifying the value in the
variable affects the closure as well.
"local a = 30" doesn't look to see if another existing local 'a' exists;
it just creates a brand new variable. This behavior only surprises me
because it is different from how JavaScript's "var" keyword behaves.