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


Groups > comp.lang.javascript > #15935

Re: Question on closure

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Question on closure
Date 2012-09-18 00:06 +0200
Organization albasani.net
Message-ID <k386sm$m64$1@news.albasani.net> (permalink)
References <e75bb2b7-b525-44d2-a957-5cf70ef25773@z8g2000yql.googlegroups.com> <XnsA0D1CBA63824Cjaejunksgooglemailco@0.0.0.50> <2rle589b4vsrkfkdahh9f78t8vrkg69t3n@4ax.com> <k37m9b$1i8$1@speranza.aioe.org> <v33f58pm2r4a20n2pkj3ieadccu0aauhle@4ax.com>

Show all headers | View raw


On 2012-09-17 22:56, Gene Wirchenko wrote:
> On Mon, 17 Sep 2012 19:23:01 +0200, Christoph Becker
> <cmbecker69@gmx.de> wrote:
>>function Counter() {
>> var theValue = 0;
>> return {
>>   getVal: function() {
>     ^^^^^^^^^^^^^^^^^^
>      Could you please give me pointer to where this syntax is
> discussed?  I tried the standard, but what I found -- sections 12.1
> Block and 12.12 Labelled Statements -- is remarkably opaque.  Do I
> even have the correct section(s)?

No, this has nothing to do with labels. The return value in this case is
an object, written in the form of an object literal. I'm sure you've
seen them before:

   var obj = {
      foo: 42,
      bar: "baz"
   };

Instead of the values 42 and "baz", you can use any valid expression,
including function expressions:

   var obj = {
      foo: 42,
      bar: function () { return "baz"; }
   };

Now you can call obj.bar() to get "baz" returned.
This is equivalent to writing -

   function myFunc () {
      return "baz";
   }

   var obj = {
      foo: 42,
      bar: myFunc
   };

- except that in this case, myFunc could be called directly as well.
Putting it all together, Christoph's example did something like this:

   return {
      foo: 42,
      bar: function () { return "baz"; }
   };

HTH.

- stefan

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Question on closure justaguy <lichunshen84@gmail.com> - 2012-09-15 13:01 -0700
  Re: Question on closure JJ <jaejunks_at@_googlemail_dot._com> - 2012-09-17 12:57 +0000
    Re: Question on closure justaguy <lichunshen84@gmail.com> - 2012-09-17 09:14 -0700
    Re: Question on closure Gene Wirchenko <genew@ocis.net> - 2012-09-17 10:02 -0700
      Re: Question on closure Christoph Becker <cmbecker69@gmx.de> - 2012-09-17 19:23 +0200
        Re: Question on closure Gene Wirchenko <genew@ocis.net> - 2012-09-17 11:13 -0700
        Re: Question on closure Gene Wirchenko <genew@ocis.net> - 2012-09-17 13:56 -0700
          Re: Question on closure JJ <jaejunks_at@_googlemail_dot._com> - 2012-09-17 21:47 +0000
          Re: Question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-18 00:06 +0200
            Re: Question on closure Gene Wirchenko <genew@ocis.net> - 2012-09-17 19:55 -0700
            Re: Question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-18 11:14 +0200
  Re: Question on closure Christoph Becker <cmbecker69@gmx.de> - 2012-09-17 19:32 +0200
    Re: Question on closure justaguy <lichunshen84@gmail.com> - 2012-09-17 11:16 -0700

csiph-web