On Oct 23, 10:33 am, Rahul <[email protected]> wrote:
> hi,
>
> i didnt understood this ,
>
> " The value x is out of bonds by the time you call the functions. So
> x
> is a closure variable, and the value of it is list.length+1 by the
> time the function finished executing. "
>
> the variable x is declared in first for-loop, so its scope is just for
> the loop.
> from my understanding array arrFun holds a function at index 0,1,2
> each.

Javascript variables have function level scope, not block level, so x
points
to the same value everywhere within buildList. The final value of x
after
the array is built is list.length+1 which in this case is 4, so what
you've
ended up with is an array of functions which all alert the value of
arrFun[4],
which is indeed undefined. The closure works like a charm, just not
the way
you expected it to. The way to get it to behave like you want is to
create a
closure for each member function of arrFun, where each member does
retain the
value of x at the time it was created. You had the right idea, the
implementation
was just a little off.

Hope that helps.

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to