Matt said:
Must this continue to be said?
While it remains true (which will be far beyond the next version of ECMA
262) it should be said and it will be said.
The fact is, there is no one definition for what an
"associative array" is. So how can you say that a js
Object is not one?
Where associative arrays exist, and use that name, a common feature is
the ability to use arbitrary keys (possibly restricted to arbitrary
string keys) to store and retrieve values. It is not safe to use
arbitrary property names with javascript objects, only sets of names
that have known characteristics (such as string keys consisting of only
upper case letters). Failing to appreciate that important distinction
risks writing something that may happily operate as expected (and even
do so for a very extended period of time) and then suddenly find itself
encountering a problematic key (that someone has introduced into the
system). That sort of problem may be hard to spot, hard to reproduce,
hard to identify, and may even only effect one language implementation.
But can be avoided entirely by thinking about javascript objects as the
things that they are (dynamically modifiable collections of name value
pairs) and not as empty vessels in which arbitrarily named values pairs
can be placed.
Depending on how you choose to define an "associative array",
an Object in javascript can most certainly be described as one.
But, as has been demonstrated in the past, such definitions of
"associative array" do not exclude objects that are obviously not
associative arrays.
IMO, most programmers looking for an "associative array" in
javascript are in fact looking for what an Object offers.
That is probably true (as most specific cases have sufficient
constraints on the property names employed for it to be possible to
determine whether they are a safe set or not). But it is also evident
that many javascript novices have been told that they have an
"associative array" in the javascript object and then had their
expectations disappointed by an object that does no more and no less
than it was intended to do (be amenable to runtime modification of (most
of) its (actual or potential)named properties).
Despite its quirks and special cases, I've never met or
heard of a single person who was looking for an associative
array who was not satisfied using an Object.
I have seen some examples posted to the group, but as I said the issues
of non-safe names may not be easy to spot so following a recommendation
to use a javascript object as an "associative array" may appear to solve
many more problems than it really does in the long term.
However, it is a pity that people "looking for an associative array"
don't state their problem in more comprehensive terms, as 'I want to
store values under key that all start with a decimal digit, or are all
uppercase" could have people pointed straight to the javascript object
as completely appropriate, while 'I want to store values using
completely arbitrary keys' could have them directed towards the many
'safe hashtable' implementations that have appeared in the group over
the years.
I propose:
<FA****RY>
Q: How do I implement an Associative Array in Javascript?
A: The term Associative Array is vague, having different
meanings in different languages, so it's impossible to
know what behavior is expected of an Associative Array
implementation. However, Javascript's Object() can behave
as a key/value storage object, which is the minimum that
most people expect from an Associative Array implementation.
<snip>
I don't think storing key value pairs in the minimum that people expect
from an associative array. There are hundreds of Java objects with named
public members in which you can store a value, and so store key value
pairs (the key may be totally pre-defined and the value type constrained
but still storing a value using a key is what assigning to such a member
constitutes). A reasonably define "associative array" would have limited
constraints on the number of key, value pairs that could be stored, and
the keys would be expected to be arbitrary.
It makes much more sense to tell people that there are no associative
arrays in javascript and then tell them what can be achieved with what
there is, and how it may be achieved.
Richard.