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


Groups > comp.lang.javascript > #8158

Fun with Arrays: What Have I Done?

From Gene Wirchenko <genew@ocis.net>
Newsgroups comp.lang.javascript
Subject Fun with Arrays: What Have I Done?
Date 2011-11-08 20:32 -0800
Organization A noiseless patient Spider
Message-ID <d40kb79urtc6ekdiubqajt8piarusksvro@4ax.com> (permalink)

Show all headers | View raw


Dear JavaScripters:

     I do not remember where I got the idea that JavaScript can handle
arrays with string indexes, but I decided to try it, because it could
simplify certain code that I am planning.

     In the course of my experiments -- KRA-KOOM! -- I came up with
the following code.  I appear to have two slightly different arrays
with some elements in common or one array with an alter ego.  Could
someone please explain why?

***** Start of Code *****
<html>

<!--
 try3.html
 Array Playaround
 Last Modification: 2011-11-08
-->

<head>
  <title>try3.html: Array Playaround</title>

<script type="text/javascript">

var Collection=new Array(3);
Collection[0]="zero";
Collection[1]="one";
Collection[2]=2;
Collection[3]="trois";
Collection["seven"]=8-1;
Collection[5]="cinq";

for (var i=0; i<Collection.length; i++)
  alert(i+":"+Collection[i]);

for (i in Collection)
  alert(i+":"+Collection[i]);

</script>

</head>

<body>

</body>

</html>
***** End of Code *****

     The first loop outputs
          0:zero
          1:one
          2:2
          3:trois
          4:undefined
          5:cinq
and the second loop outputs
          0:zero
          1:one
          2:2
          3:trois
          5:cinq
          seven:7

     What exactly did I do, please?
 
Sincerely,

Gene Wirchenko

Back to comp.lang.javascript | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Fun with Arrays: What Have I Done? Gene Wirchenko <genew@ocis.net> - 2011-11-08 20:32 -0800
  Re: Fun with Arrays: What Have I Done? RobG <rgqld@iinet.net.au> - 2011-11-08 21:30 -0800
    Re: Fun with Arrays: What Have I Done? RobG <rgqld@iinet.net.au> - 2011-11-08 21:44 -0800
    Re: Fun with Arrays: What Have I Done? Dr J R Stockton <reply1145@merlyn.demon.co.uk> - 2011-11-10 20:01 +0000
  Re: Fun with Arrays: What Have I Done? Tim Streater <timstreater@greenbee.net> - 2011-11-09 10:57 +0000
    Re: Fun with Arrays: What Have I Done? SteveYoungTbird <stephen.young@chello.at> - 2011-11-09 12:17 +0100
    Re: Fun with Arrays: What Have I Done? Elegie <elegie@anonymous.invalid> - 2011-11-09 12:24 +0100
      Re: Fun with Arrays: What Have I Done? Tim Streater <timstreater@greenbee.net> - 2011-11-09 13:31 +0000
    Re: Fun with Arrays: What Have I Done? Gene Wirchenko <genew@ocis.net> - 2011-11-09 10:32 -0800

csiph-web