Quote:
Originally Posted by gits
hi ...
i think i have to mention some things here. the code above misuses an array as an object ... try:
you will get 0. what you do with the above assignments is to create member-variables within the array-object itself, you could also do:
in case you want to loop now ... you MUST use:
- for (var i in table) {
-
alert(table[i]);
-
}
since you use now the array-object! this is quite a misuse of it. try to push an element and now our 'normal' array will contain 1 element.
the correct way would be to use an object at all.
and use that as a 'quasi'-assoc-array ... but it is an object :) ... in javascript we have no assoc-arrays ... but we may use objects for that.
kind regards
I don't agree that there is any abuse going on otherwise the syntax would not be allowed. I know that elements created using associative syntax are stored differently, which is why I didn't use a loop or try to read a .length property. The purpose was to let the interpreter effectively do the looping, and I could just as easily have written:
- var table={red:'#FF0000', green:'#00FF00', blue:'#0000FF'};
which works equally well with the following statement and I'm sure is treated the same way internally.