Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8307853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:37:53+00:00 2026-06-08T18:37:53+00:00

I would like to add key-value pairs of metadata to arbitrary JavaScript objects. This

  • 0

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

JSON.stringify(obj) === JSON.stringify(obj.WithMetaData('key', 'value'))

MetaData aware code should be able to retrieve the data by key, i.e.

obj.WithMetaData('key', 'value').GetMetaData('key') === 'value'

Is there any way to do it – in node.js? If so, does it work with builtin types such as String and even Number? (Edit Thinking about it, I don’t care about real primitives like numbers, but having that for string instances would be nice).

Some Background: What I’m trying to do is cache values that are derived from an object with the object itself, so that

  • to meta data unaware code, the meta data enriched object will look the same as the original object w/o meta
  • code that needs the derived values can get it out of the meta-data if already cached
  • the cache will get garbage collected alongside the object

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.

(btw clojure has this feature: http://clojure.org/metadata)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:37:54+00:00Added an answer on June 8, 2026 at 6:37 pm

    You can use ECMA5’s new object properties API to store properties on objects that will not show up in enumeration but are nonetheless retrievable.

    var myObj = {};
    myObj.real_property = 'hello';
    Object.defineProperty(myObj, 'meta_property', {value: 'some meta value'});
    for (var i in myObj)
        alert(i+' = '+myObj[i]); //only one property - @real_property
    alert(myObj.meta_property); //"some meta value"
    

    More information here: link

    However you’re not going to be able to do this on primitive types such as strings or numbers, only on complex types.

    [EDIT]

    Another approach might be to utilise a data type’s prototype to store meta. (Warning, hack ahead). So for strings:

    String.prototype.meta = {};
    String.prototype.addMeta = function(name, val) { this.meta[name] = val; }
    String.prototype.getMeta = function(name) { return this.meta[name]; };
    var str = 'some string value';
    str.addMeta('meta', 'val');
    alert(str.getMeta('meta'));
    

    However this is clearly not ideal. For one thing, if the string was collected or aliased (since simple data types are copied by value, not reference) you would lose this meta. Only the first approach has any mileage in a real-world environment, to be honest.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to add a small dice-rolling effect to my Javascript code. I
(Using Oracle) I have a table with key/value pairs like this: create table MESSAGE_INDEX
I would like to protect one key/value pair in my appSettings but not the
I have own project and i would like add for this class same as
Is there any .NET type that would represent a set of key-value pairs where
Hi I'm new to python. I am trying to add different key value pairs
I'd like to store a set of key/value pairs in the application settings of
I would like to have a HashMap with only one key-value object. I have
I have an integer column which I would like to add a foreign key
I have a std::map, and I would like to add a valid key to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.