Look at this issue about improving the closure explanation in JS. It’s not yet been worked on, but I’m willing to now, and I’d love to see your ideas on how we could improve the closures explanation in JS.
An issue I was wondering about is this section: if I’m right, isn’t this a block? Why, then, does the explanation speak about a function-scoped variable and a nested lexical scope (there is no nesting as I see it)? Perhaps it assumes some global “function” in which the block is nested?
Function-scope
The var
keyword defines a function-scoped variable. This means that variables defined by var
are available anywhere in the function where they are defined and any nested lexical scope (either function or block).
{
var five = 5;
}
// This will succeed because 'five' exists in the same scope
var tenTimes = five * 10;
Other open ideas from the GH issue include having an advanced explanation with a function being returned and dropping the block example (if I understood @SleeplessByte’s comment correctly). Do you agree with these, and if so, how exactly would we go about implementing them, e.g. what would we have instead of the block example?