3

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?

2
  • 1
    For objects, yes. For strings and numbers and booleans, no. You can always create your own map structure I guess, but I wouldn't call that "metadata" unless I really wanted to for some reason :-) Commented Jan 20, 2013 at 21:18
  • 1
    Functions are objects in JavaScript, so yes. Commented Jan 20, 2013 at 21:23

2 Answers 2

3

For objects, including functions, the best way to implement get/setMetadata is not to implement it at all.

double = function(a){ return a*2; };
double.desc = "Returns the double of a number."
alert(double.desc);

For "primitive" strings/numbers you can use a dictionary approach like suggested in another answer:

metaStorage = {}

setMetaData = function(obj, data) {
    if(typeof obj == "object")
        obj._metaData = data;
    else
        metaStorage[obj] = data;
}

getMetaData = function(obj) {
    if(typeof obj == "object")
        return obj._metaData;
    else
        return metaStorage[obj];
}

setMetaData(1, "this is one");
console.log(getMetaData(1))


setMetaData(window, "my window");
console.log(getMetaData(window))

However, I can't imagine how it could be useful to attach metadata to string/number literals.

Sign up to request clarification or add additional context in comments.

7 Comments

I was not aware you could use functions as objects, so that's the solution.
@Dokkat: Everything, apart from primitive values, is an object.
Btw, just FYI, you would not be able to distinguish between 1 and "1" or true and "true" with this method. That's a limitation of property names really (everything is converted to a string).
Also, you couldn't distinguish between identical strings that are actually different things. For example, two texts could contain an identical phrase, but you could want to tag them pointing to their respective article.
@FelixKling: yes, a more realistic solution should use a nested dict and something like storage[typeof x][x]=data, but again, I fail to see any point in attaching data to primitives.
|
0

You could do this :

var metaDataStorer = {};
function addMetadata(object, meta) {
    metaDataStorer[object] = meta;
}
function getMetadata(object) {
    return metaDataStorer[object];
}

2 Comments

Object properties have to have string-valued property names (the property values can be anything, but the names have to be strings, in other words).
Libert, try making more than one object and console.log your metaDataStorer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.