Is this a closure?

Discussion in 'Javascript' started by donarb@nwlink.com, Nov 8, 2006.

  1. Guest

    About a week ago I was on a web page (which I can't find now) that had
    some tricky javascript. I remember that some of the top-level object
    definitions were surrounded by braces, I assume they were closures, but
    I can't find any information online about this particular syntax. It
    reminds me of something in Damien Conway's OO Perl book, what he calls
    a shared lexical. Is it a closure and what would the benefit be?


    This is a sample of what I recall it looked like:

    {
    function myObject() {}
    myObject.prototype.dosomething = function() {}
    myObject.prototype.dosomethingelse = function() {}
    }
    , Nov 8, 2006
    #1
    1. Advertising

  2. wrote:
    > About a week ago I was on a web page (which I can't find now) that had
    > some tricky javascript. I remember that some of the top-level object
    > definitions were surrounded by braces, I assume they were closures, but
    > I can't find any information online about this particular syntax. It
    > reminds me of something in Damien Conway's OO Perl book, what he calls
    > a shared lexical. Is it a closure and what would the benefit be?
    >
    > This is a sample of what I recall it looked like:
    >
    > {
    > function myObject() {}
    > myObject.prototype.dosomething = function() {}
    > myObject.prototype.dosomethingelse = function() {}
    > }


    It would be better if you could post what it actually looked like
    rather than what you recall. There are no closures formed in this code.
    It is an ordinarily looking definition of a constructor with
    assignments to its - prototype - property, but unexpectedly (and
    pointlessly) surrounded by a Block Statement.

    Richard.
    Richard Cornford, Nov 8, 2006
    #2
    1. Advertising

  3. VK Guest

    wrote:
    > About a week ago I was on a web page (which I can't find now) that had
    > some tricky javascript. I remember that some of the top-level object
    > definitions were surrounded by braces, I assume they were closures, but
    > I can't find any information online about this particular syntax. It
    > reminds me of something in Damien Conway's OO Perl book, what he calls
    > a shared lexical. Is it a closure and what would the benefit be?


    While the recall you provided doesn't look right, my best guess would
    be that you saw a code using CC (Cornford-Crockford) scope management.
    On the first glance it looks as weird as you tried to depict :)

    Take a look at
    <http://www.crockford.com/javascript/private.html>
    and especially at
    <http://www.litotes.demon.co.uk/js_info/private_static.html>
    if it reminds you something.
    VK, Nov 8, 2006
    #3
    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. JezB

    Page Closure

    JezB, Dec 3, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    419
    Cowboy \(Gregory A. Beamer\)
    Dec 3, 2003
  2. Ivan Sutton
    Replies:
    0
    Views:
    342
    Ivan Sutton
    Oct 1, 2003
  3. Simon
    Replies:
    3
    Views:
    5,238
    John C. Bollinger
    Jan 12, 2005
  4. bugbear
    Replies:
    11
    Views:
    584
    bugbear
    Sep 5, 2005
  5. Julian Mehnle
    Replies:
    0
    Views:
    223
    Julian Mehnle
    Jul 17, 2003
Loading...

Share This Page