Enumeration

The for in statement can loop over all of the property names in an object. The enumeration will include functions and prototype properties.

  1. var fruit = {
  2. apple: 2,
  3. orange:5,
  4. pear:1
  5. },
  6. sentence = 'I have ',
  7. quantity;
  8. for (kind in fruit){
  9. quantity = fruit[kind];
  10. sentence += quantity+' '+kind+
  11. (quantity===1?'':'s')+
  12. ', ';
  13. }
  14. // The following line removes the trailing coma.
  15. sentence = sentence.substr(0,sentence.length-2)+'.';
  16. // I have 2 apples, 5 oranges, 1 pear.