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


Groups > comp.lang.javascript > #31496

Re: How to write object-oriented JS function?

From John Harris <niam@jghnorth.org.uk.invalid>
Newsgroups comp.lang.javascript
Subject Re: How to write object-oriented JS function?
Date 2016-10-03 10:26 +0100
Organization A noiseless patient Spider
Message-ID <6194vb178d81b2mrgg8viikpgoqusvv9il@4ax.com> (permalink)
References <78daa021-c64d-4017-b761-17ea13fdcaee@googlegroups.com> <44hvub55bdcc6ca2lcivhfu3qs1ss39b1g@4ax.com> <1dee4fbb-163e-40d4-b901-9dedeacc3e92@googlegroups.com> <b6o1vbd9kulajd8ou11eoegdtnrdrg0jmr@4ax.com> <097196c0-218d-4cce-8eb7-13e47fac5468@googlegroups.com>

Show all headers | View raw


On Sun, 2 Oct 2016 11:27:07 -0700 (PDT), justaguy
<lichunshen84@gmail.com> wrote:

  <snip>
>One issue tho.  Previously without namespace, I set up global vars, for instance, 
>cnt = 1;
>function addMoreRows() {
>  // set business rule, no more than 20 new entries (TRs)
>  if (cnt == 20) { return } 
>  cnt += 1;
>  ...
>}
>
>But now, no matter where I define the global counter var of cnt (out of my namespace or inside, my business role of 
> if (cnt == 20) { return }  
>is no longer valid (yes I also tried to replace cnt with this.cnt inside each function, still to no avail).  How do we deal with this?

It looks as though cnt isn't used anywhere outside the function, so
declare it as a local variable, so making it a variable that doesn't
exist outside the function :

  jag.addMoreRows() = function ()
    {
       var cnt = 1;
       ...
       if (cnt == 20) 
         return;
       ...
    }

And cn likewise in the other function.

Incidentally, if the current value of cnt needed to be read in more
than one function, then the namespace object would hold it :
  jag.cnt = 1;
  if (jag.cnt == 1)
  etc.
'this' values have nothing to do with it.

  John

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


Thread

How to write object-oriented JS function? justaguy <lichunshen84@gmail.com> - 2016-09-30 09:55 -0700
  Re: How to write object-oriented JS function? John Harris <niam@jghnorth.org.uk.invalid> - 2016-10-01 15:16 +0100
    Re: How to write object-oriented JS function? justaguy <lichunshen84@gmail.com> - 2016-10-01 09:23 -0700
      Re: How to write object-oriented JS function? John Harris <niam@jghnorth.org.uk.invalid> - 2016-10-02 11:27 +0100
        Re: How to write object-oriented JS function? justaguy <lichunshen84@gmail.com> - 2016-10-02 04:22 -0700
        Re: How to write object-oriented JS function? justaguy <lichunshen84@gmail.com> - 2016-10-02 11:27 -0700
          Re: How to write object-oriented JS function? John Harris <niam@jghnorth.org.uk.invalid> - 2016-10-03 10:26 +0100
            Re: How to write object-oriented JS function? John Harris <niam@jghnorth.org.uk.invalid> - 2016-10-03 10:38 +0100
              Re: How to write object-oriented JS function? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-10-03 12:01 +0200
                Re: How to write object-oriented JS function? John Harris <niam@jghnorth.org.uk.invalid> - 2016-10-03 19:07 +0100
                Re: How to write object-oriented JS function? justaguy <lichunshen84@gmail.com> - 2016-10-03 14:33 -0700
                Re: How to write object-oriented JS function? John Harris <niam@jghnorth.org.uk.invalid> - 2016-10-04 17:59 +0100
                Re: How to write object-oriented JS function? justaguy <lichunshen84@gmail.com> - 2016-10-04 20:10 -0700
                Re: How to write object-oriented JS function? justaguy <lichunshen84@gmail.com> - 2016-10-05 04:47 -0700
                Re: How to write object-oriented JS function? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-10-03 23:46 +0200
                Re: How to write object-oriented JS function? $Bill <news@todbe.com> - 2016-10-03 15:22 -0700
                Re: How to write object-oriented JS function? $Bill <news@todbe.com> - 2016-10-03 15:28 -0700
                Re: How to write object-oriented JS function? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-10-04 09:16 +0200
                Re: How to write object-oriented JS function? $Bill <news@todbe.com> - 2016-10-04 00:46 -0700
                Re: How to write object-oriented JS function? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-10-04 14:56 +0200
                Re: How to write object-oriented JS function? John Harris <niam@jghnorth.org.uk.invalid> - 2016-10-04 16:49 +0100
                Re: How to write object-oriented JS function? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-10-04 19:55 +0200

csiph-web