Arrays disguised as objects

L

Laser Lips

I was messing about with accossiative arrays when I discovered that an
accossiative array does not have a length.

var myAsso = [];
myAsso["one"]="one";
myAsso["two"]="two";
myAsso["three"]="three";
alert(myAsso.length); //=0

I see that arrays can have mixed key attributes of both numbers
(index) and strings (accossiative), but the array's length will only
be counted using the indexed based keys.

var myArr=[];
myArr[0]="my value";
myArr[1]="my value";
myArr[2]="my value";
myArr["3"]="my value"; //this one is still classed as an index
myArr["a"]="my value";
myArr["b"]="my value";
myArr["c"]="my value";
myArr["d"]="my value";

alert("Array length = " + myArr.length); //length = 4


for(var x=0;x<myArr.length;x++)
{
alert("for x) "+x + " = "+myArr[x]);
}
for(var n in myArr)
{
alert("for in) "+n + " = " + myArr[n]);
}

myArr[4]="my value";

alert("Array length = " + myArr.length);

myArr["e"]="my value";

alert("Array length = " + myArr.length);

myArr.test=function()
{
alert("Func");
}

myArr[5]="my value";

alert("Array length = " + myArr.length);

myArr.test();

/////////////////////////

//now the same again but define the variable as an object
var myObj={};

myObj[0]="my value";
myObj[1]="my value";
myObj[2]="my value";
myObj["3"]="my value";
myObj["a"]="my value";
myObj["b"]="my value";
myObj["c"]="my value";
myObj["d"]="my value";

alert("Array length = " + myObj.length);

for(var x=0;x<myObj.length;x++)
{
alert("for x) "+x + " = "+myObj[x]);
}
for(var n in myObj)
{
alert("for in) "+n + " = " + myObj[n]);
}

myObj[4]="my value";

alert("Array length = " + myObj.length);

myObj["e"]="my value";

alert("Array length = " + myObj.length);

myObj.test=function()
{
alert("Func");
}

myObj[5]="my value";

alert("Array length = " + myObj.length);

myObj.test();
 
J

Joe Nine

Laser said:
I was messing about with accossiative arrays when I discovered that an
accossiative array does not have a length.

var myAsso = [];
myAsso["one"]="one";
myAsso["two"]="two";
myAsso["three"]="three";
alert(myAsso.length); //=0

I see that arrays can have mixed key attributes of both numbers
(index) and strings (accossiative), but the array's length will only
be counted using the indexed based keys.

var myArr=[];
myArr[0]="my value";
myArr[1]="my value";
myArr[2]="my value";
myArr["3"]="my value"; //this one is still classed as an index

I don't think it's "classed" as being an index, I expect it's being
converted to an index of 3. If you used different strings instead of
always "my value" I expect you'd find myArr[3] == myArr["3"].
 
L

Laser Lips

Laser said:
I was messing about with accossiative arrays when I discovered that an
accossiative array does not have a length.
var myAsso = [];
myAsso["one"]="one";
myAsso["two"]="two";
myAsso["three"]="three";
alert(myAsso.length);  //=0
I see that arrays can have mixed key attributes of both numbers
(index) and strings (accossiative), but the array's length will only
be counted using the indexed based keys.
   var myArr=[];
   myArr[0]="my value";
   myArr[1]="my value";
   myArr[2]="my value";
   myArr["3"]="my value"; //this one is still classed as an index

I don't think it's "classed" as being an index, I expect it's being
converted to an index of 3. If you used different strings instead of
always "my value" I expect you'd find myArr[3] == myArr["3"].

Please ignore this thread and go to the other one I just posted ;-)
 
T

Thomas 'PointedEars' Lahn

Joe said:
Laser said:
I was messing about with accossiative arrays when I discovered that an
accossiative array does not have a length.

var myAsso = [];
myAsso["one"]="one";
myAsso["two"]="two";
myAsso["three"]="three";
alert(myAsso.length); //=0

I see that arrays can have mixed key attributes of both numbers
(index) and strings (accossiative), but the array's length will only
be counted using the indexed based keys.

var myArr=[];
myArr[0]="my value";
myArr[1]="my value";
myArr[2]="my value";
myArr["3"]="my value"; //this one is still classed as an index

I don't think it's "classed" as being an index, I expect it's being
converted to an index of 3. If you used different strings instead of
always "my value" I expect you'd find myArr[3] == myArr["3"].

The proper explanation that can be derived from, if not actually found in
the ECMAScript Specification is:

All property names are strings, whereas with the bracket property accessor
syntax the expression between the brackets is converted to a primitive
string value first.

Array instances are only special in that property names for which
ToString(ToUint32(propertyName)) === propertyName and
ToUint32(propertyName) != 2^32-1 applies, are regarded indexes of array
elements which can affect the encapsulating Array instance's `length'
property.

As a result, the creation of properties that do not fit these requirements
(like those named "one", "two", or "three") does not cause a change in
array length. In short: There are no built-in associative arrays in
ECMAScript implementations like JavaScript. (If you need such a thing, you
need to define your own object, for example a collection implementation.)

We have discussed this so often that I must be very much mistaken if it is
not already covered by a FAQ entry. (In the opposite case, this posting
may be used as basis for such an entry.)


HTH

PointedEars
 
M

Michael Haufe (\TNO\)

I was messing about with accossiative arrays when I discovered that an
accossiative array does not have a length.

var myAsso = [];
myAsso["one"]="one";
myAsso["two"]="two";
myAsso["three"]="three";
alert(myAsso.length);  //=0

Think of it mentally like this:

var myAsso = [];
[].one = "one"
[].two = "two"
[].three = "three"
myAsso.length yields 0 because the array is still empty, you just
tacked properties on the object, not in the object

In Gecko browsers you can do this:

var myAsso = ["foo","bar"];
myAsso["one"]="one";
myAsso["two"]="two";
myAsso["three"]="three";

console.log(myAsso.__count__ )//3 (counts the enumerable object
properties plus the array members)
console.log(myAsso.length) //2 (counts the members of the array)

Note that this is possible ONLY in FireFox and its relatives.
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top