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


Groups > comp.lang.javascript > #31493

Re: How to write object-oriented JS function?

Newsgroups comp.lang.javascript
Date 2016-10-02 11:27 -0700
References <78daa021-c64d-4017-b761-17ea13fdcaee@googlegroups.com> <44hvub55bdcc6ca2lcivhfu3qs1ss39b1g@4ax.com> <1dee4fbb-163e-40d4-b901-9dedeacc3e92@googlegroups.com> <b6o1vbd9kulajd8ou11eoegdtnrdrg0jmr@4ax.com>
Message-ID <097196c0-218d-4cce-8eb7-13e47fac5468@googlegroups.com> (permalink)
Subject Re: How to write object-oriented JS function?
From justaguy <lichunshen84@gmail.com>

Show all headers | View raw


On Sunday, October 2, 2016 at 6:27:39 AM UTC-4, John Harris wrote:
> On Sat, 1 Oct 2016 09:23:12 -0700 (PDT), justaguy
> <liskkds@gmail> wrote:
> 
> >On Saturday, October 1, 2016 at 10:17:03 AM UTC-4, John Harris wrote:
> >> On Fri, 30 Sep 2016 09:55:22 -0700 (PDT), justaguy
> >> <lZ@skskail.com> wrote:
>   <snip> 
> >> Creating a single object to hold all the functions is ok, but it would
> >> be a namespace implementation, not OO.
> >> 
> >> 
> >>   <snip>
> >> >	function addRow4Prop() {
>   <snip> 
> >> >	function addRow4Name() {
> 
> 
>   <snip>
> >I should have stated, these two functions add TABLE elements (TRs and TDs and FORM elements such as INPUT) dynamically to the page in question.  Thus, value for newly created INPUT field etc. are unknown (they would be user input).
> 
> You haven't said what prompts these additions to the tables. Is it
> just once when the page is first displayed, or whenever the user does
> a certain action?
> 
> 
> >And I like your idea of "Creating a single object to hold all the functions is ok, but it would be a namespace implementation, ", but questions, (a) since I have different TABLES such as TBL1 and TBL2 and new elements need to be added into them respectively, how do we create a single object to reference each of them? First, reference such TABLE as parent.TBL1 ?
> >(b) "namespace implementation" not OO would be fine as long as it's a better way to get things done.
> 
> A namespace implementation would have you writing something like
> jag.addRow4Prop() instead of just addRow4Prop(), and jag.addRow4Name()
> instead of just addRow4Name(). That way, if you add code to your
> program that uses different functions also named addRow4Prop or
> addRow4Name then they won't get confused. At worst you just have to
> change the name jag. The need for this change would be a lot easier to
> spot than for lots of different functions, and you can spot it before
> getting very strange faults in your program.
> 
> jag, if that's what you call it, is a global object with the functions
> as its properties. jag knows nothing. It's just a parking place for
> functions that would otherwise be global.
> 
> As to whether it's a better way to get things done, it depends what
> you think is useful. It's tidier, and makes it easier to build large
> programs, especially if different people do different parts of it.
> 
>   John

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?

Thanks.

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