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();
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();