Re: OT: TECH: JavaScript help
From: | Mark J. Reed <markjreed@...> |
Date: | Monday, December 22, 2008, 15:33 |
On Mon, Dec 22, 2008 at 9:35 AM, Benct Philip Jonsson <bpj@...> wrote:
> options, but IIANM JavaScript knows nothing of hashes.
In Javascript, every object can have arbitrarily-named attributes with
arbitrary values. In other words, every object is essentially already
a hash. You can create an object and initialize it with a set of
attributes using the curly brace syntax:
var x = { 'foo': 'bar', 'donut': 'woohoo' }
alert("x's donut is " + x['donut']);
when the key is a literal string that matches the rules for a name in
Javascript (leading letter or underscore followed by nothing but more
letters and underscores and maybe some digits), you can also use the
dot notation:
alert("x's donut is " + x.donut)
but for anything else, including variable keys, you have to use the brackets:
var key = 'vooba vooba vooba'
x[key] = 'ding!';
alert("x's " + key + " is " + x[key]);
The drawback here is that methods are also stored as attributes, which
means you can overwrite them with your values if you use an object as
a generic hash. If you use the Prototype Javascript library, it offers
real hashes with keys separate from object attributes, but you have to
put up with method calls to set and retrieve values instead of
assignment and indexing.
So you want three different strings associated with each option - the
one displayed in the drop-down, the actual value submitted to the
form, and the contents of an adjacent label field? Do you have an
example using all three strings?
--
Mark J. Reed <markjreed@...>