Re: Can someone please explain this code?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Can someone please explain this code?

From: Richard Cornford <Richard@litotes.demon.co.uk>
Date: Mon Jan 30 2006 - 23:03:47 CET

amerar@iwc.net wrote:
> I'm not good at Javascript, so I am trying to understand
> this small bit of code:
>
> var groups=document.$fm.category.options.length;
> var group=new Array(groups);
> for (i=0; i<groups; i++);
> group[i]=new Array();
>
>
> It looks like line #1 is getting the length of a combo
> box on the page.

The length property of the - options - collection of a SELECT element
(there are no 'combo boxes' in HTML).

> Line #2 then creates an array called 'group' that has
> max elements from the value in #1.

It is not a maximum length. Javascript Arrays have a maximum length, but
it is the same for all arrays (and is quite big).

> What could line #3

Spinning its wheels. A javascript - for - statement controls the
execution of a single statement. That single statement may be a block
statement, which may then contain numerous other statements, or it could
be any other valid statement in the language. The statement controlled
by this - for - loop is an empty statement, as define by the semicolon
following the closing parenthesis. This is almost certainly an error.

> & #4 be doing?

Because the loop has uselessly executed by the time line 4 is executed
the - i - variable is equal to - groups -, and so a new element is added
to the - group - array at index - i - and a reference to a new Array
assigned as the value of that element.

> The array has already been created with the size........I'm
> unclear.

It is an attempt to make an array of arrays written by someone
unfamiliar with javascript, and inevitably unsuccessful as a result.

Richard.
Received on Tue Feb 7 21:27:59 2006