Is it possible to add metadata to JavaScript objects (including strings, numbers and functions)? That is,
double = function(a){ return a*2; };
addMetadata(double,{desc:"Returns the double of a number."});
getMetadata(double).desc;
How could addMetadata and getMetadata be implemented?
For objects, including functions, the best way to implement get/setMetadata is not to implement it at all.
For “primitive” strings/numbers you can use a dictionary approach like suggested in another answer:
However, I can’t imagine how it could be useful to attach metadata to string/number literals.