JRS: In article <1147456577.491857.305590@y43g2000cwc.googlegroups .com>
, dated Fri, 12 May 2006 10:56:17 remote, seen in
news:comp.lang.javascript,
andre.roberge@gmail.com posted :[color=blue]
>I'm using a js script (approx. 300 lines) from someone else to create
>dynamically some web pages, using Python. Unfortunately, I am not
>familiar with javascript :-(
>
>I now want to reproduce the same functionality given by the script at
>multiple points in the page. Basically, the script goes like this
>
>/* whole bunch of functions with things in them like
>input.value
>input.focus()
>input.selectionEnd
>*/
>
>At the very end, I have
>function init() {
>input = document.getElementById("input");
>}
>
>with the appropriate html code (with id="input") appearing on the web
>page.
>
>One obvious way to do it is to generate, from Python, multiple copies
>of the entire script with [input] replaced everywhere by [input1,
>input2, input3, ...], but this doesn't strike me as a very smart thing
>to do.
>
>I have read the faq, and didn't see anything helpful (with the
>exception perhaps of a long list of potential sites to wade through,
>not really aware of what I could be looking for.) Any ideas, pointers
>to specific web sites where such topics are covered, etc., would be
>much appreciated.[/color]
Where code is substantially repetitive, use a function call for each
instance with parameters to customise it.
Those three lines, albeit unreasonable as posted, could be written as
function Tintin(champs) {
champs.value
champs.focus()
champs.selectionEnd }
TinTin(input)
which is of course longer; but now each repeat adds one line instead of
three.
The reason that it is not in the FAQ is probably that using functions /
methods / subroutines for repeated code is a fundamental part of
programming, independent of language. But sections 4.5 4.10 4.15 4.16
4.22 illustrate such use, or at least the provision of such functions.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.