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


Groups > comp.lang.javascript > #10152

Re: Array syntax

Message-ID <2453001.kcvsFY6v6J@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-01-12 00:26 +0100
Subject Re: Array syntax
Newsgroups comp.lang.javascript
References <8bcdc6d4-cf3b-4af3-901d-8464de4de2f2@k10g2000yqk.googlegroups.com> <4f0c025c$0$30150$a8266bb1@newsreader.readnews.com>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Denis McMahon wrote:

> On Mon, 09 Jan 2012 14:11:11 -0800, DrKen wrote:
> 
> var locations = $("#field-location select:first option");
> var loc = new Array();
> loc["az"] = { value: "AZ", caption: "Azusa" };
> loc["co"] = { value: "CO", caption: "Colorado Springs" };
> loc["hd"] = { value: "VI", caption: "High Desert Regional Center" };
> loc["ie"] = { value: "IE", caption: "Inland Empire Regional Center" };
> loc["in"] = { value: "IN", caption: "International" };
> loc["la"] = { value: "LA", caption: "Los Angeles Regional Center" };
> loc["mu"] = { value: "MU", caption: "Murrieta Regional Center" };

(sic!)

Is this your code or are you quoting the OP?  Watch for the quotation 
levels.

It does not make a lot of sense to use an `Array' instance here.  The 
properties of `Array' instances are not employed here.  The array data 
structure is _not_ added elements with indexes "az", "co" etc., but the 
`Array' instance is *augmented* with *properties* named "az", "co" etc.

As a result, the value of `loc.length' remains 0 (evidence that there are no 
built-in associative arrays in ECMAScript implementations).  Indeed, should 
for some reason a *perceived* "index" be the name of a built-in property of 
`Array' instances or their prototype, unexpected behavior would be the 
result.  For example:

  /* RangeError: Invalid array length */
  loc["length"] = { value: "AZ", caption: "Azusa" };

(because there is no Object.prototype.toValue() and 
Object.prototype.toString() returns "[object Object]" for `Object' 
instances, which is numerically converted to NaN.)

So it should be

  var log = new Object();

or more simple

  var log = {};

(as already used) instead.  However, it is not very efficient to add 
properties on initialization that way.  As already used, properties can be 
defined in the Object initializer/literal, and initializers can be nested:

  var log = {
    az: { value: "AZ", caption: "Azusa" },
    …
  };

Obviously, if the data were generated by the server, a conversion of the 
server-side data to JSON (for example, from PHP with json_encode()) would be 
the simplest way to make it available to the client-side code (JSON differs 
here only in that it requires the property name to be quoted, which is also 
valid ECMAScript).
 

PointedEars
-- 
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
  -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

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


Thread

Array syntax DrKen <javajedi2@yahoo.com> - 2012-01-09 14:11 -0800
  Re: Array syntax "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-01-10 00:58 +0200
  Re: Array syntax Denis McMahon <denismfmcmahon@gmail.com> - 2012-01-10 09:18 +0000
    Re: Array syntax "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-01-10 15:36 +0000
    Re: Array syntax Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-12 00:26 +0100

csiph-web