I would like to add key-value metadata pairs to arbitrary JavaScript objects. This metadata should not affect code that does not know metadata, which means, for example,
JSON.stringify(obj) === JSON.stringify(obj.WithMetaData('key', 'value'))
Code supporting MetaData should be able to retrieve data using a key, i.e.
obj.WithMetaData('key', 'value').GetMetaData('key') === 'value'
Is there a way to do this - in node.js? If so, does it work with built-in types such as String and evenNumber ? ( Change ). I think that I'm not interested in real primitives, such as numbers, but it would be nice to have this for string instances).
Some information: what I'm trying to do is cache values that are produced from an object with the object itself, so
- for metadata that does not know the code, an object enriched with metadata will look the same as the original object without meta
- code that needs derived values can infer it from metadata if it is already cached
- the cache will receive garbage collected near the object
Another way is to store a hash table with caches somewhere, but you will never know when an object receives garbage collection. Each instance of the object will need to be taken care of manually so that caches do not leak.
(btw clojure has this function: http://clojure.org/metadata )
source
share