Run into an odd issue, perhaps best explained by code:
var fruits = ["apples", "oranges", "pears"];
var Breakfast = {
  _consume : function (fruit) {
    Breakfast[fruit]._numConsumed++;
  }
};
for (var f in fruits) {
  var fruit = fruits[f];
  Breakfast[fruit] = {
    consume : function () {
      Breakfast._consume(fruit);
    },
    _numConsumed: 0
  }
}
Breakfast.pears.consume();
Breakfast.pears.consume();
Breakfast.apples.consume();
Breakfast.apples.consume();
Breakfast.apples.consume();
console.log("Pears eaten: " + Breakfast.pears._numConsumed);
console.log("Apples eaten: " + Breakfast.apples._numConsumed);
The result of this is:
$ node example.js
Pears eaten: 5
Apples eaten: 0
Not quite sure how to overcome this behaviour?
Have I coded something wrong? Or is there a different pattern I should be using? (given that I want the "consume" function to be available to all my fruits etc)
Many thanks!
 
     
     
     
    