Randy Webb wrote:
NO! An Object in JS is *NOT* an associative array. JS doesn't have an
associative array - period.
Must this continue to be said?
The fact is, there is no one definition for what an "associativ e array" is.
So how can you say that a js Object is not one?
Depending on how you choose to define an "associativ e array", an Object in
javascript can most certainly be described as one.
IMO, most programmers looking for an "associativ e array" in javascript are
in fact looking for what an Object offers. 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 propose:
<FAQENTRY>
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.
A simple example:
var assocArray = {};
assocArray["key"] = "value";
alert(assocArra y["key"]);
for (var key in assocArray) {
alert(key + " = " + assocArray[key]);
}
Using a Javascript Object() as an Associative Array comes with the following
limitations:
1. Keys must be strings, not arbitrary objects.
2. Some keys are invalid, because Javascript's Object has built-in
properties that cannot be over-written.
3. Some keys will exist by default because of Object's built-in properties.
These will not be enumerated in for..in loops but can be directly accessed.
4. No method or property exists by default to retrieve the size - or number
of keys - currently stored.
</FAQENTRY>
--
Matt Kruse
http://www.JavascriptToolbox.com http://www.AjaxToolbox.com