title: Is there some way to add meta-data to JavaScript objects?
body: <p>I would like to add key-value pairs of metadata to arbitrary JavaScript objects. This metadata should not affect code that is not aware of the metadata, that means for example</p>
  
  <pre><code>JSON.stringify(obj) === JSON.stringify(obj.WithMetaData('key', 'value'))
  </code></pre>
  
  <p>MetaData aware code should be able to retrieve the data by key, i.e.</p>
  
  <pre><code>obj.WithMetaData('key', 'value').GetMetaData('key') === 'value'
  </code></pre>
  
  <p>Is there any way to do it - in node.js? If so, does it work with builtin types such as <code>String</code> <strike>and even <code>Number</code></strike>? (<strong>Edit</strong> Thinking about it, I don't care about real <em>primitives</em> like numbers, but having that for <em>string</em> instances would be nice).</p>
  
  <p>Some Background: What I'm trying to do is cache values that are derived from an object with the object itself, so that</p>
  
  <ul>
  <li>to meta data unaware code, the meta data enriched object will look the same as the original object w/o meta </li>
  <li>code that needs the derived values can get it out of the meta-data if already cached</li>
  <li>the cache will get garbage collected alongside the object</li>
  </ul>
  
  <p>Another way would be to store a hash table with the caches somewhere, but you'd never know when the object gets garbage collected. Every object instance would have to be taken care of manually, so that the caches don't leak.</p>
  
  <p>(btw clojure has this feature: <a href="http://clojure.org/metadata" rel="noreferrer">http://clojure.org/metadata</a>)</p>