Bart Van der Donck wrote:
Richard Cornford wrote:
>[...]
Javascript does not have "associativ e arrays" [...]
Or "hashes".
[...]
"In JavaScript an object is a mapping from property names
to values -- that is, an associative array."
http://en.wikipedia.org/wiki/Associa...ays#JavaScript
If wikipedia wish to define an "associativ e array" as "a mapping from
property names to values" (or more generally as 'a mapping of keys to
values') then that is their choice. So much of what associative arrays
actually are in practice is disregarded in that definition as to render
it trivial. Under that definition many things become "associativ e
arrays", some of which would be better never to be thought of as
"associativ e arrays".
Dictionary definitions of "array" tend to stress ordering in the
arrangement (and we have heard of the expectation of ordering (in some
sense) in "associativ e arrays" from the OP) yet javascript objects have
no ordering of their properties (except as a coincidental manifestation
of particular object implementations ).
When an "associativ e array" is just created/instantiated in a language
that supports such it would be expected to be empty (or just have the
key/value pairs specified at creation), while the javascript Object is
never 'empty', and so cannot be assumed to not posses a value mapped to
an arbitrary key just because no such key/value pair has been assigned.
Many "associativ e array" in a language that supports such have some
(recoverable) notion of the number of key/value pairs assigned, while
javascript objects have no interest in the number or properties they
contain.
The practice of talking of either javascript Objects or Arrays as
"associativ e arrays" tends to introduce in the minds of the readers who
are familiar with actual associative arrays from other languages an set
of expectations that are not true (or not generally true) of javascript
Objects/Arrays. Inevitably false expectations about javascript will not
be satisfied by javascript, will tend to get in the way of an accurate
understanding of javascript, and will directly result in
issues/problems/bugs in code written to those expectations. The most
reasonable response to this situation is to state clearly that
javascript objects are not "associativ e arrays" and so allow the reader
to move on to the much more productive consideration of what a
javascript Object actually is.
Richard.