What is function closure?

Discussion in 'Javascript' started by LAN MIND, Jan 27, 2008.

  1. LAN MIND

    LAN MIND Guest

    ?
     
    LAN MIND, Jan 27, 2008
    #1
    1. Advertising

  2. LAN MIND

    Guest

    function your_function()
    {
    var closure='hello function';
    return function()
    {
    alert(cosure);
    }
    }

    On Jan 27, 4:24 pm, LAN MIND <> wrote:
    > ?
     
    , Jan 27, 2008
    #2
    1. Advertising

  3. LAN MIND

    Guest

    sorry, that one line should be:
    alert(closure);

    That should answer your question if it were about how to make a
    closure. If your question was actually asking what it was (in a what
    does it do sense), then a closure is a function that has access to
    various temporary variables that were created in the same scope that
    they were (see example below).

    On Jan 27, 7:26 pm, "" <> wrote:
    > function your_function()
    > {
    > var closure='hello function';
    > return function()
    > {
    > alert(cosure);
    > }
    >
    > }
    >
    > On Jan 27, 4:24 pm, LAN MIND <> wrote:
    >
    > > ?
     
    , Jan 27, 2008
    #3
  4. From the "JavaScript: The Definitive Guide" book:

    «JavaScript functions are a combination of code to be executed and the
    scope in which to execute them. This combination of code and scope is
    known as a closure (...) All JavaScript functions are closures. These
    closures are only interesting, however, in the case discussed above:
    when a nested function is exported outside the scope in which it is
    defined. When a nested function is used in this way, it is often
    explicitly called a closure.»
     
    Marcio Faustino, Jan 27, 2008
    #4
  5. LAN MIND

    timothytoe Guest

    On Jan 27, 3:01 am, Marcio Faustino <> wrote:
    > From the "JavaScript: The Definitive Guide" book:
    >
    > «JavaScript functions are a combination of code to be executed and the
    > scope in which to execute them. This combination of code and scope is
    > known as a closure (...) All JavaScript functions are closures. These
    > closures are only interesting, however, in the case discussed above:
    > when a nested function is exported outside the scope in which it is
    > defined. When a nested function is used in this way, it is often
    > explicitly called a closure.»


    One common use is when SetTimeout is called. Suppose you set up a
    timer which will do something in 10 seconds. You don't have to worry
    about keeping the scope around to be used because the closure holds on
    to the scope long enough for the SetTimeout to use it.
     
    timothytoe, Jan 27, 2008
    #5
  6. Marcio Faustino wrote:
    > From the "JavaScript: The Definitive Guide" book:


    -- a book that is only recommended in the FAQ as it is the least bad book
    about the subject --

    > «JavaScript functions are a combination of code to be executed and the
    > scope in which to execute them. This combination of code and scope is
    > known as a closure (...) All JavaScript functions are closures.


    I don't think that statement in its generality is correct. A closure in
    programming is defined as a program function that reproduces its definition
    context when executed. In JavaScript, or rather ECMAScript implementations,
    that would only apply to functions, or rather methods, that refer to
    properties of their definition context.

    > These closures are only interesting, however, in the case discussed above:
    > when a nested function is exported outside the scope in which it is
    > defined.


    Incorrect. A nested function does not need to be exported in order to form
    a closure that would be of value:

    function Foo(x)
    {
    this.bar = function()
    {
    return x - 19;
    };
    }

    window.alert((new Foo(42)).bar());

    > When a nested function is used in this way, it is often explicitly called
    > a closure.»


    It creates a closure then, and is not merely (explicitly) called so.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, Jan 27, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Ng
    Replies:
    1
    Views:
    347
    Bengt Richter
    Mar 11, 2005
  2. Replies:
    2
    Views:
    204
    Steven D'Aprano
    Mar 25, 2007
  3. Replies:
    9
    Views:
    277
    Dennis Lee Bieber
    Apr 17, 2007
  4. SeanMon

    Function closure inconsistency

    SeanMon, Jul 23, 2010, in forum: Python
    Replies:
    3
    Views:
    253
    Terry Reedy
    Jul 23, 2010
  5. Julian Mehnle
    Replies:
    0
    Views:
    246
    Julian Mehnle
    Jul 17, 2003
Loading...

Share This Page