Richard Cornford wrote:
mscir wrote:
<snip> So we have to use one of JavaScript's minor mysteries.
Minor mystery or standard language construct?
In JavaScript,
objects are also associative arrays (or hashes).
Objects are not necessarily either associative arrays or hashes (in
the senses that the terms may be used in other languages). Objects may be
implemented using associative arrays or hashes in whatever language
the implementation is written in but the language specification is not
interested in how its requirements are implemented. Objects are only
required to be _dynamic_ associations of named properties with
values. And indeed evidence suggests that the IE/JScript object
implementation is a list of some sort.
The important detail is that ECMAScript has no Array accessing syntax
at all, only property accessors (one type of which resemble array
accessing syntax from other languages). And that ECMAScript Array objects are
just Objects that implement additional behaviour when properties are
accessed (when the property names sufficiently resemble 32 bit unsigned
integers) and have additional methods defined on a custom Array.prototype.
Speaking of ECMAScript objects in terms of "associative arrays" and
"hashes" tends to obscure the real nature of those objects and lead
to misconceptions about what behaviour can be expected of ECMAScript
objects.
As you correctly note, there is often a problem of misconception that
arises when the term "associative array" chances into the vicinity of
ECMAScript. However, in my view, the "real nature of objects" gets
completely obscured when their relationship to "associative arrays" is
tossed.
The "real nature" of ECMAScript Object objects is that they are simply
enhanced associative arrays. The enhancement comes through creation of
a linkage (the prototype chain) of underlying associative arrays. In
the case of the fundamental Object object, the chain is wonderfully
short, and the ramifications upon key-based lookup entirely explainable
and readily comprehensible.
While you have provided all kinds of very valuable information in the
above, it seems to me that anyone who is not fully familiar with
ECMAScript objects characteristics would think that they are totally
foreign and full of wonderful, mysterious construction, and of which
the uninitiated are yet become fully cognizant. And that just
constitutes a different form of obscuration, which I would quite truly
think would not be your intent.
Regards,
../rh