Saturday, 24 August 2013

Get calling arguments for getter in javascript

Get calling arguments for getter in javascript

Given a javascript object like this:
var myThing = {};
Object.defineProperty(myThing, 'gen', {
'get' : function() {
// access caller name here, so I can return cool/neat stuff
}
});
I want to be able to get children of myThing.gen, but know what is being
asked for in the getter.
for example:
var coolThing = myThing.gen.oh.cool;
var neatThing = myThing.gen.oh.neat;
I want the "oh.cool" or "oh.neat" part in getter, so I can make decisions
based on this, and return something specific to it. I am ok with solution
not working in IE, or old browsers, as it is primarily for node.
without actually solving the problem of dynamically discovering the
caller, I can do this:
var myThing = {};
Object.defineProperty(myThing, 'gen', {
'get' : function() {
return {
'oh':{
'cool':'super-cool',
'neat':'pretty neat'
}
};
}
});
Is there a way to do this dynamically?

No comments:

Post a Comment