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


Groups > comp.lang.javascript > #8572

Re: Create a multi-dimensional array

Message-ID <3003096.SPkdTlGXAF@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2011-11-23 21:33 +0100
Subject Re: Create a multi-dimensional array
Newsgroups comp.lang.javascript
References <1c993ed8-05a6-43b4-a28f-e9b0ad1c6241@c16g2000pre.googlegroups.com> <4ecd3ca5$0$28487$a8266bb1@newsreader.readnews.com>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Denis McMahon wrote:

> var a5 = new Array(2);
> for (var i=0; i<2; i++)
> {
> a5[i]=new Array(2);
> for (var j=0; j<2; j++) a5[i][j]=new Array(2);
> }

Using arguments for the Array constructor is pointless here and error-prone.  
For that matter, using the Array constructor is unnecessary and potentially 
error-prone.  Use the Array initializer instead; it is most certainly 
ubiquitous by now [1]:

  var a5 = [];
  for (var i = 0; i < 2; ++i)
  {
    a5[i] = [];
    for (var j = 0; j < 2; ++j)
    {
      a5[i][j] = [];
    }
  }

Note that you can save memory if you do not create all potentially needed 
Array instances, but you have to pay for that with a bit of runtime (tests 
before access whether there already is an Array instance reference 
assigned).


PointedEars
___________
[1] <http://PointedEars.de/es-matrix/#!>
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
  -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

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


Thread

Create a multi-dimensional array Archos <raul.san@sent.com> - 2011-11-23 03:04 -0800
  Re: Create a multi-dimensional array "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-11-23 13:41 +0200
    Re: Create a multi-dimensional array John G Harris <john@nospam.demon.co.uk> - 2011-11-23 16:41 +0000
    Re: Create a multi-dimensional array Dr J R Stockton <reply1147@merlyn.demon.co.uk> - 2011-11-24 18:42 +0000
      Re: Create a multi-dimensional array "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-11-25 01:53 +0200
        Re: Create a multi-dimensional array Gene Wirchenko <genew@ocis.net> - 2011-11-24 17:15 -0800
  Re: Create a multi-dimensional array Erwin Moller <Since_humans_read_this_I_am_spammed_too_much@spamyourself.com> - 2011-11-23 12:46 +0100
    Re: Create a multi-dimensional array Archos <raul.san@sent.com> - 2011-11-23 06:11 -0800
  Re: Create a multi-dimensional array Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-23 18:34 +0000
    Re: Create a multi-dimensional array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-23 21:33 +0100

csiph-web