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


Groups > comp.lang.javascript > #31483

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-01 15:16 +0100
Organization A noiseless patient Spider
Message-ID <44hvub55bdcc6ca2lcivhfu3qs1ss39b1g@4ax.com> (permalink)
References <78daa021-c64d-4017-b761-17ea13fdcaee@googlegroups.com>

Show all headers | View raw


On Fri, 30 Sep 2016 09:55:22 -0700 (PDT), justaguy
<lichunshen84@gmail.com> wrote:

>Hi,
>
>The following two JS functions works perfectly fine, however, they are not object-oriented.  Fyi, each function inserts TR or TRs and their children of TDs with or without attributes into certain position in a defined TABLE.
> 
>I wonder if folks who are truly JavaScript experts can turn them into better JS code (object-oriented and code re-use, use closure etc. when appropriate).  Many thanks.

Warning : Beware creating a single DoTheJob object with just one
method, DoIt. That wouldn't be OO, it would be a confusing and messy
repackaging of a function. 

Creating a single object to hold all the functions is ok, but it would
be a namespace implementation, not OO.


  <snip>
>	function addRow4Prop() {
  <snip>	
>				// cell 1
>                var cell = document.createElement("td");				
>				var cellText = document.createElement('input');
>				cellText.type = 'text';
>				cellText.id = 'documentType'+cnt;
>				cellText.name = 'documentType'+cnt;
>				cellText.size = "60";
>                cell.appendChild(cellText);
>                newrow.appendChild(cell);
>		
>				// cell 2
>				var cell = document.createElement("td");
>                var cellText = document.createElement('input');
>				cellText.type = 'text';
>				cellText.id = 'insNumPage'+cnt;
>				cellText.name = 'insNumPage'+cnt;
>				cellText.size = "15";
>                cell.appendChild(cellText);
>                newrow.appendChild(cell);

The  // cell n  code looks like a candidate for another function that
is called several times.


  <snip>
>	function addRow4Name() {
  <snip>	
>				// cell 1
>                var cell = document.createElement("td");				
>				var cellText = document.createElement('input');
>				cellText.type = 'text';
>				cellText.id = 'nameSearched'+cn;
>				cellText.name = 'nameSearched'+cn;
>				cellText.size = "60";
>                cell.appendChild(cellText);
>                newrow.appendChild(cell);
>		
>				// cell 2
>				var cell = document.createElement("td");
>                var cellText = document.createElement('input');
>				cellText.type = 'checkbox';
>				cellText.id = 'allClear'+cn;
>				cellText.name = 'allClear'+cn;
>				cellText.value = '1';
>				cellText.size = "1";
>                cell.appendChild(cellText);
>                newrow.appendChild(cell);
  <snip>

Ditto.

In general, it's not clear where the values you put in the table come
from. If they're constants why not do it in HTML. If they're data it's
the data that might be Object Oriented, though I suspect it's just a
2-D array sent by the web server. Inside the web server there's much
more scope for using objects, but that's somewhat OT here.

  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