Re: JavaScript associative arrays not ordered?
- From: "Richard Cornford" <Richard@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 19 Aug 2006 15:36:30 +0100
Bart Van der Donck wrote:
Richard Cornford wrote:
[...]
Javascript does not have "associative 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/Associative_arrays#JavaScript
If wikipedia wish to define an "associative 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 "associative
arrays", some of which would be better never to be thought of as
"associative 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 "associative arrays" from the OP) yet javascript objects have
no ordering of their properties (except as a coincidental manifestation
of particular object implementations).
When an "associative 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 "associative 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
"associative 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 "associative arrays" and so allow the reader
to move on to the much more productive consideration of what a
javascript Object actually is.
Richard.
.
- Follow-Ups:
- Re: JavaScript associative arrays not ordered?
- From: Bart Van der Donck
- Re: JavaScript associative arrays not ordered?
- References:
- JavaScript associative arrays not ordered?
- From: Rene Nyffenegger
- Re: JavaScript associative arrays not ordered?
- From: Richard Cornford
- Re: JavaScript associative arrays not ordered?
- From: Bart Van der Donck
- JavaScript associative arrays not ordered?
- Prev by Date: Re: Finding line numbers of HTML elements
- Next by Date: Re: JavaScript associative arrays not ordered?
- Previous by thread: Re: JavaScript associative arrays not ordered?
- Next by thread: Re: JavaScript associative arrays not ordered?
- Index(es):
Relevant Pages
|