Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #11721 > unrolled thread

Why is this a closure?

Started by"Tom de Neef" <tdeneef@qolor.nl>
First post2012-02-12 13:07 +0100
Last post2012-02-12 18:57 -0800
Articles 5 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  Why is this a closure? "Tom de Neef" <tdeneef@qolor.nl> - 2012-02-12 13:07 +0100
    Re: Why is this a closure? Jake Jarvis <pig_in_shoes@yahoo.com> - 2012-02-12 14:10 +0100
      Re: Why is this a closure? Jake Jarvis <pig_in_shoes@yahoo.com> - 2012-02-12 14:31 +0100
      Re: Why is this a closure? "Tom de Neef" <tdeneef@qolor.nl> - 2012-02-12 14:42 +0100
        Re: Why is this a closure? RobG <rgqld@iinet.net.au> - 2012-02-12 18:57 -0800

#11721 — Why is this a closure?

From"Tom de Neef" <tdeneef@qolor.nl>
Date2012-02-12 13:07 +0100
SubjectWhy is this a closure?
Message-ID<4f37ab8b$0$6874$e4fe514c@news2.news.xs4all.nl>
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: to keep a function's 
execution context alive by passing a reference to a local function. But 
that's not what is done here. Can you explain pls or point me to some text 
that will explain. The Google references I've checked seem to indicate that 
this is not a closure, albeit there is one that says that any function 
within a function will be a closure.
Tom

//code
function test(){
"use strict";
  function compare(context,id){
    return context==id;
  }
  function check(context,id){
    return compare(context,id) || compare(context,id.toLowerCase());
  }
  check('dummy','DUMMY');
}



[toc] | [next] | [standalone]


#11722

FromJake Jarvis <pig_in_shoes@yahoo.com>
Date2012-02-12 14:10 +0100
Message-ID<9pps33FfffU1@mid.uni-berlin.de>
In reply to#11721
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.

-- 
Jake Jarvis

[toc] | [prev] | [next] | [standalone]


#11723

FromJake Jarvis <pig_in_shoes@yahoo.com>
Date2012-02-12 14:31 +0100
Message-ID<9ppt8vFp3dU1@mid.uni-berlin.de>
In reply to#11722
On 12.02.2012 14:10, Jake Jarvis wrote:
> 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.
>

Just to clarify, that is just what JSLint means with that word in the 
context of its report, it's not a definition for what the word 'closure' 
means outside of that particular context for most people.

For a non-JSLint-report-specific definition you could look at

http://www.jibbering.com/faq/notes/closures/#clDefN

-- 
Jake Jarvis

[toc] | [prev] | [next] | [standalone]


#11724

From"Tom de Neef" <tdeneef@qolor.nl>
Date2012-02-12 14:42 +0100
Message-ID<4f37c1d4$0$6929$e4fe514c@news2.news.xs4all.nl>
In reply to#11722
"Jake Jarvis" <pig_in_shoes@yahoo.com> schreef in bericht 
news:9pps33FfffU1@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? I do not see how it binds local 
variables or why it shows that "Closures are one of the most powerful 
features of ECMAScript". To me the examplecound come straight from Algol 60, 
which doesn't know about closures at all. That's why I am confused.
Tom 

[toc] | [prev] | [next] | [standalone]


#11735

FromRobG <rgqld@iinet.net.au>
Date2012-02-12 18:57 -0800
Message-ID<b2e83c5b-d680-48ca-84c6-b71639c67ad9@jn12g2000pbb.googlegroups.com>
In reply to#11724
On Feb 12, 11:42 pm, "Tom de Neef" <tden...@qolor.nl> wrote:
> "Jake Jarvis" <pig_in_sh...@yahoo.com> schreef in berichtnews:9pps33FfffU1@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web