Order of Key-Value pair ("associative arrays")

T

Thomas 'PointedEars' Lahn

rh said:
Grant said:
John said:
It's the other way round. Every ECMAScript object is an associative
array.

No, every ECMAScript object is an object that can have properties which can be
accessed using dot notation (theObject.theProperty), or "array" notation
(theObject[theProperty]).

The original statement is correct. The accessor syntax, while being
important, doesn't change the fundamental (logical) nature of the
object.

The fundamental (logical) nature of any object is not to be an "associative
array" (whatever that might be) but to be an object having properties and
methods, encapsulating data. Only because ECMAScript is a loosely typed
language where properties can be added and removed during runtime and those
of type "function" are considered methods of the object, does not make an
object per se an array. You cannot inherit from arrays, for example.


PointedEars
 
L

Lee

Thomas 'PointedEars' Lahn said:
Full ACK. To me, an "array" in computer science and thus programming is
an *ordered* data structure (AIUI an "array" outside of computer science
has the property of order as well), composed of *elements*. That means,
one can access element #x or append elements at the end of to the array
directly, without iteration.

Where do you get the requirement of being able to find the end
without iteration? Ever program in C?

With a simple non-Array[1] ECMAScript object there are no such means
available. You cannot access property #x unless you indexed it before
and you cannot obtain the number of properties until you iterated the
object using a for-in-loop. And even then you cannot tell for sure that
these are all properties of the object (which would be then elements of
the "associative array" if you like call them so) because some may have
the NonEnum flag set and thus do not occur during iteration but yet are
present. And single properties can be read-only. I don't know of any
implementation that calls itself an "array" and that allows that kind
of data hiding and level of data integrity protection regarding the
elements of that "array".

The fact that an associative array doesn't fit the
definition of an array shouldn't be so disturbing.

Your argument reminds me of somebody complaining that
the term "magnetic field" is improper because a "field"
is something that you can plant corn in.

There is certainly nothing that precludes an associative
array from also implementing private fields, etc.
 
R

rh

Thomas 'PointedEars' Lahn said:
rh said:
Grant said:
John G Harris wrote:
It's the other way round. Every ECMAScript object is an associative
array.

No, every ECMAScript object is an object that can have properties which can be
accessed using dot notation (theObject.theProperty), or "array" notation
(theObject[theProperty]).

The original statement is correct. The accessor syntax, while being
important, doesn't change the fundamental (logical) nature of the
object.

The fundamental (logical) nature of any object is not to be an "associative
array" (whatever that might be) but to be an object having properties and
methods, encapsulating data.

As described earlier in this thread, an associative array is a
well-defined mapping of keys to values. In computing, they are an
abstract data type, which means they are defined by the operations
they support, rather than in terms of their underlying implementation.
They can also be considered to be a form of generalization of an array
which supports only integers as indexes.
Only because ECMAScript is a loosely typed
language where properties can be added and removed during runtime and those
of type "function" are considered methods of the object, does not make an
object per se an array.

No, but it does mean that an objects ECMAScript are conceptually based
on associative arrays. The keys are "property names" and the values
are scalars or other objects, such as functions. And while some
language implementations may have confined associative array values to
being scalars, and/or all values must be of the same type, there's
nothing inherent in the concept of associative arrays that requires
such constraints to prevail.

Since dynamic addition and removal of keys/values is fundamental to
associative array operation, and you recognize the fact that
ECMAScript supports such for objects, you're simply lending
confirmation to the contention that ECMAScript objects are in fact,
fundamentally, associative arrays.

../rh
 
R

rh

Lee said:
Your argument reminds me of somebody complaining that
the term "magnetic field" is improper because a "field"
is something that you can plant corn in.

Would that perhaps be the same person that claimed to have Grade 12
equivalency because he had gone through Grade 6 twice? :).

../rh
 
T

Thomas 'PointedEars' Lahn

rh said:
Thomas 'PointedEars' Lahn [...] wrote [...]...
rh wrote:
Only because ECMAScript is a loosely typed
language where properties can be added and removed during runtime and those
of type "function" are considered methods of the object, does not make an
object per se an array.

No, but it does mean that an objects ECMAScript are conceptually based
on associative arrays.

Obviously you have not the slightest idea of OOP
or of ECMAScript and its implementations as OOLs.


PointedEars
 
R

rh

Thomas said:
rh said:
Thomas 'PointedEars' Lahn [...] wrote [...]...
rh wrote:
Only because ECMAScript is a loosely typed
language where properties can be added and removed during runtime and those
of type "function" are considered methods of the object, does not make an
object per se an array.

No, but it does mean that an objects ECMAScript are conceptually based
on associative arrays.

Obviously you have not the slightest idea of OOP
or of ECMAScript and its implementations as OOLs.

True or otherwise, whatever, that statement would constitute yet
another red herring pulled from your red herring fishnet[1] and ever
so deftly laid across the trail.

../rh

[1] Red herring fishnet: a capturing device used by those with unusual
perspectives, most often consisting of a bunch of holes tied together
with string. When constructed as a rectangle, the device can actually
appear to be an array.
 
G

Grant Wagner

Marek said:
Douglas said:
Can one rely on the order of keys inserted into an associative Javascript
array? For example:

var o = new Object();

o["first"] = "Adam";
o["second"] = "Eve";
o["third"] = "Cane";
o["fourth"] = "Abel";
No. The ECMAScript specification allows the contents of an object to be
unordered. Most implementations do order the contents, but you should
not rely on that.

Yes, whilst IE seems to preserve ordering, Opera7 definately doesnt.

One way to preserve order in these cases is to create an array that keeps track
of the keys in the order you want. It also lets you initialize the object's
properties pretty easily.

var o = {};
var keys = [ "first", "second", "third", "fourth" ];
var values = [ "Adam", "Eve", "Cane", "Abel" ];
var len = keys.length;
while (len-- > ) {
o[keys[len]] = values[len];
}

for (var i = 0; i < keys.length; i++) {
document.write(o[keys] + "<br>");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top