Gregc. said:
Hi
I am having trouble understanding for loops. If I have a loop that
says:
var coffee = new Array();
coffee ["mixedblend"] = 5.50;
That uses an Array as a plain object, you are not using any of its
special array properties. You could have written:
var coffee = new Object();
coffee ["mixedblend"] = 5.50;
Though the use of an initialiser or object literal is generally
preferred:
var coffee = { mixedblend : 5.50 };
for (c in coffee)
{code goes here}
for (c in coffee ){
alert( coffee[c] );
}
That for saying while there is a c in coffee, then conduct the code.
Is that a correct interpretation?
Almost - change "while there is a c" to "for each enumerable property".
It is usually called a for..in loop to differentiate it from a
conditional for loop. A for..in loop goes over all the enumerable
properties of the object in no particular order (some implementations
will go over them in the order they were added, it is not reliable).
Most built-in objects like the JavaScript Array object have many
non-enumerable properties (e.g. you can't use for..in to get the length
property of an Array).
It is impossible for you to add non-enumerable properties, or to make
the non-enumerable ones enumerable (though you can mask them to give
the appearance of enumerability).