On Feb 12, 11:42 pm, "Tom de Neef" <
tden...@qolor.nl> wrote:
> "Jake Jarvis" <
pig_in_sh...@yahoo.com> schreef in berichtnews:9pps33...@mid.uni-berlin.de...
>
>
>
>
>
> > On 12.02.2012 13:07, Tom de Neef wrote:
> >> When I run the code below through JSLint, the report says:
> >> Global test
> >> 1 test()
> >> Closure compare
> >> ...
>
> >> I thought I had understood the concept of closure:
> > <snip>
>
> > From JSLint's manual
>
> >
http://www.jslint.com/lint.html#report
>
> > | Report
> > |
> > | If JSLint is able to complete its scan, it generates a function
> > | report. It lists for each function:
> > | ...
> > | /Closure/: The variables and parameters that are declared in the
> > | function that are used by its inner functions.
>
> And is that then indeed what a closure is?
For some, that is sufficient. But I think most javascript programmers
require a definition more like that in the FAQ: the closure needs to
persist beyond the life of the outer function, e.g.
var isJustDigits = (function() {
var re = /^\d+$/;
return function(n) {
return re.test(n);
}
}
where the function assigned to isJustDigits() retains a closure to the
variable re. It could also be said that even if there was no re
variable involved, the returned function has a closure to the
activation object of the outer function and the global object. But
those details are not usually considered a closure.
> I do not see how it binds local
> variables or why it shows that "Closures are one of the most powerful
> features of ECMAScript".
They are, but your example is minimalist at best.
> To me the examplecound come straight from Algol 60,
> which doesn't know about closures at all. That's why I am confused.
And why for most a closure requires a bit more than scoping rules. :-)
--
Rob