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


Groups > comp.lang.javascript > #15924

Re: Question on closure

From Christoph Becker <cmbecker69@gmx.de>
Newsgroups comp.lang.javascript
Subject Re: Question on closure
Date 2012-09-17 19:23 +0200
Organization Aioe.org NNTP Server
Message-ID <k37m9b$1i8$1@speranza.aioe.org> (permalink)
References <e75bb2b7-b525-44d2-a957-5cf70ef25773@z8g2000yql.googlegroups.com> <XnsA0D1CBA63824Cjaejunksgooglemailco@0.0.0.50> <2rle589b4vsrkfkdahh9f78t8vrkg69t3n@4ax.com>

Show all headers | View raw


Gene Wirchenko wrote:
> Could you please post the rest?  How do you access the value?

Try the following variation of JJ's code:

function Counter() {
 var theValue = 0;
 return {
   getVal: function() {
     return theValue;
   },
   increase: function() {
     return ++theValue;
   },
   decrease: function() {
     return --theValue;
   }
 };
}

It can be used in the following way:

var c1 = Counter(), c2 = Counter();
c1.getVal()   ===> 0
c1.increase() ===> 1
c1.getVal()   ===> 1
c2.getVal()   ===> 0

-- 
Christoph M. Becker

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