[thelist] [javascript] object literal question

liorean liorean at gmail.com
Thu Mar 9 09:55:19 CST 2006

On 09/03/06, Matt Warden <mwarden at gmail.com> wrote:
> liorean wrote:
> > Well, I don't agree on this. The object and the array are essentially
> > the same in implementation.
> Maybe they are essentially or effectively the same, but what I am saying
> is that they are not the same.
> Array.prototype.foobar = function() { alert('hi'); }
> (new Array()).foobar(); // will work
> (new Object()).foobar(); // won't work
> vs.
> Object.prototype.foobar = function() { alert('hi'); }
> (new Object()).foobar(); // will work
> (new Array()).foobar(); // will work

Well, Array inherits from Object, so obviously Object doesn't inherit
from Array. I hardly see how that is relevant to the discussion, since
the associative array features of JavaScript are all from Object.
Object could as well have been named HashTable or AssocArray, and have
a subtype Array which provides special behaviour for just
keys-which-can-be-converted-to-positive-integers and related
functionality as members on the prototype.

> > 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...
> Again, this is the same in many languages, like PHP. These other
> languages just don't smack you in the face with it by allowing you to
> syntactically treat object properties as hash keys like you can in
> JavaScript.

Which only extends the argument to those languages... in fact, any
languages that use soft typed, dynamically sized, single dimensional,
string associative arrays. But that's not my point, that was just
something I threw in as an example of of this:

If you are going to argue that something is one way or the other, you
have to make clear your terminology. In this case, exactly what do you
mean by "associative array"? I've tried to explain my stance here.
Arrays provide nothing of the associative array behavior in
JavaScript, all that is provided by objects. So, either both of them
are associative arrays or none of them. Arrays are just a subcase of
associative arrays in JavaScript.
David "liorean" Andersson

More information about the thelist mailing list