2

Let's say I have the object:

var Animals = {
  cow: {
    id: 1,
    value: "moo"
  },
  chicken: {
    id: 2,
    value: "cluck"
  }
};

What I'd like to do override the toString method on all the properties (cow, chicken, etc.). I know I can do it individually, but is there something to cover all properties in one call? Something like:

Animals.properties.prototype.toString = function() {
  return this.value;
}
JoshBramlett
  • 723
  • 1
  • 6
  • 11
  • If you create your animal instances from a common Animal prototype using constructor functions you xould : http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Oct 30 '14 at 23:33

1 Answers1

3

The only thing you can do is iterate over the object's properties:

for (var prop in Animals) {
    Animals[prop].toString = ...;
}
Huangism
  • 16,278
  • 7
  • 48
  • 74
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143