Why is this a closure?

Discussion in 'Javascript' started by Tom de Neef, Feb 12, 2012.

  1. Tom de Neef

    Tom de Neef Guest

    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');
    }
     
    Tom de Neef, Feb 12, 2012
    #1
    1. Advertisements

  2. Tom de Neef

    Tom de Neef Guest

    "Jake Jarvis" <> schreef in bericht
    news:-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
     
    Tom de Neef, Feb 12, 2012
    #2
    1. Advertisements

  3. Tom de Neef

    RobG Guest

    On Feb 12, 11:42 pm, "Tom de Neef" <> wrote:
    > "Jake Jarvis" <> schreef in berichtnews:-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
     
    RobG, Feb 13, 2012
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Why, why, why???

    =?Utf-8?B?VGltOjouLg==?=, Jan 27, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    1,150
    Juan T. Llibre
    Jan 27, 2005
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    856
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    VWD why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    666
    =?Utf-8?B?Q2lhcmFuIE8nJycnRG9ubmVsbA==?=
    Dec 21, 2006
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,585
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger

    why why why usercontrol

    Mr. SweatyFinger, Nov 29, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    530
    Steve B.
    Nov 29, 2006
  6. Mr. SweatyFinger
    Replies:
    2
    Views:
    3,049
    Smokey Grindel
    Dec 2, 2006
  7. Skybuck Flying
    Replies:
    16
    Views:
    1,050
    tragomaskhalos
    Aug 25, 2007
  8. Julian Mehnle
    Replies:
    0
    Views:
    519
    Julian Mehnle
    Jul 17, 2003
Loading...