Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #31484
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2016-10-01 09:23 -0700 |
| References | <78daa021-c64d-4017-b761-17ea13fdcaee@googlegroups.com> <44hvub55bdcc6ca2lcivhfu3qs1ss39b1g@4ax.com> |
| Message-ID | <1dee4fbb-163e-40d4-b901-9dedeacc3e92@googlegroups.com> (permalink) |
| Subject | Re: How to write object-oriented JS function? |
| From | justaguy <lichunshen84@gmail.com> |
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:
>
> >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
John,
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).
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.
Thanks.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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