[thelist] [javascript] object literal question

liorean liorean at gmail.com
Wed Mar 8 23:41:55 CST 2006


On 09/03/06, Matt Warden <mwarden at gmail.com> wrote:
> liorean wrote:
> > That's what an object literal is. All objects are in effect
> > associative arrays in JavaScript.
> Not quite. Object properties are implemented as an associative array (as
> they are in many languages, whether the languages expose that fact to
> the programmer or not), but an object is not an array (but, of course,
> an array is an object).

Well, I don't agree on this. The object and the array are essentially
the same in implementation. The only difference that isn't just a
question of properties on the prototype is the way it dynamically
increases the length property if the arrray is given new members that
have indices that can be translated to positive integers, and the way
it removes all members that have indices that if translatable to
positive integers are larger than the new value for the length
property when that property is set.

> I think you meant this, because you said:
> > There's no need to use an array if
> > you aren't taking advantage of the array specific methods and
> > behaviors, especially the dynamically changing length property.
> but I just wanted to clear that up in case anyone else is listening :)

Well, one could argue that JavaScript doesn't have any array type at
all if one wish. They are after all hash-tables or some variation of
linked-lists in their implementation and not really arrays...

If you describe "associative array" as a type of object that has
properties that are accessible by key, then objects in JavaScript are
associative arrays. If you describe "associative array" as a type of
object where string key as well as integer index members are treated
as array, in the respect of being treated the same, then there exists
neither any implementation nor any possibility of natively
implementing associative arrays in JavaScript implementations.



There's really nothing of arrayness to "associative arrays" in the
form of JavaScript array objects, these members are not in any way
treated differently from how they are treated in object literals, so I
wouldn't say a JavaScript array is any more an associative array than
an object is.
--
David "liorean" Andersson
<uri:http://liorean.web-graphics.com/>



More information about the thelist mailing list