31605

Set prototype for all object properties

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; }

Answer1:

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

for (var prop in Animals) { Animals[prop].toString = ...; }

Recommend

  • Finding max of list in lists and returning a separate list value python
  • LocalStorage not working with Windows 8 Phone HTML 5 project template
  • In jQuery how do I create alert every 5 seconds?
  • URL with query string not matching react-router route
  • Java 6 Map.get() Type Safety Unexpected Behavior(?) [duplicate]
  • PHP's SimpleXMLElement analog for Node.js [closed]
  • How to use the string.match method to find multiple occurrences of the same word in a string?
  • Problems compiling DLL using GCC on Windows
  • Can't make a vector of a class containing a ptr_vector
  • Is ESP as general-purpose as EAX?
  • Merging two backbone collection and models into one object using underscore
  • Confusion with collections of nested generics
  • +entityForName: could not locate an entity named 'Dogs' in this model.
  • PHP: concatenation of multidimensional array elements
  • Reshaping pandas dataframe from categorical to counts
  • Update nested_attributes by default
  • GQL: Find all entities that contain a substring
  • How do can I get XmlRoot to have a collection of objects?
  • C++ #include Loop
  • Creating JS objects in PHP with commas in between
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Javascript convert timezone issue
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • jquery mobile loadPage not working
  • AT Commands to Send SMS not working in Windows 8.1
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • using HTMLImports.whenReady not working in chrome
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • How to stop GridView from loading again when I press back button?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • failed to connect to specific WiFi in android programmatically