Reputation: 2221
I'm trying to return the name of the variable that a function is assigned to.
I have included an example below. The end result is I would like modelPerson.title()
to return the variable name title
.
For example, I have the following code:
Defining some base model types
var types = {
string: function() {
return function() {
return "I want this to return 'title'";
}
}
};
Using the model types
var modelPerson = {
title: types.string(),
firstName: types.string(),
surname: types.string(),
position: types.string()
};
Trying to return the title
console.log(modelPerson.title());
Sorry if this is a little unclear. I have included a JSFiddle if it helps: http://jsfiddle.net/4f6VE/
Thanks for any help you can give
Upvotes: 6
Views: 1417
Reputation: 13570
That's actually possible, but involves some v8 specific stuff:
var types = {
string: function() {
return function() {
var obj = {};
var prepare = Error.prepareStackTrace;
Error.prepareStackTrace = function (_, stack) {
return stack
}
Error.captureStackTrace(obj)
var method = obj.stack[0].getMethodName();
Error.prepareStackTrace = prepare;
return method;
}
}
};
var modelPerson = {
title: types.string(),
firstName: types.string(),
surname: types.string(),
position: types.string()
};
console.log(modelPerson.title());
console.log(modelPerson.firstName());
but you probably should use something less insane
Upvotes: 7
Reputation: 147413
I'm trying to return the name of the variable that a function is assigned to
You can't, not reliably. Several variables or properties can reference the same object, and some objects exists without ever being assigned to a variable (such as function expressions without a name that are called immediately).
Upvotes: 1
Reputation: 16726
here is a method that can work in strict mode (without deprecated arguments.callee or proprietary arguments.callee.caller properties), using your code with minimal re-factoring and no hard-coded names:
var types={
string: function types(){
return function me() {
for(var it in this){
if(me==this[it]) return it;
}
};
}
};
var modelPerson = {
title: types.string(),
firstName: types.string(),
surname: types.string(),
position: types.string()
};
alert( modelPerson.title() ); // shows: "title"
alert( modelPerson.surname() ); // shows: "surname"
Upvotes: 0
Reputation: 664569
The end result is I would like modelPerson.title() to return the variable name title.
Then use something like this:
function define(obj, name) {
obj[name] = function() {
return name;
};
}
var modelPerson = {};
define(modelPerson, "title");
define(modelPerson, "firstName");
define(modelPerson, "surname");
define(modelPerson, "position");
// … - a loop maybe?
> console.log(modelPerson.title());
"title"
Upvotes: 0
Reputation: 4048
I don't really know what is this for, but
var modelPerson = {
title : function title(){ return arguments.callee.name; },
firstName : function firstName(){ return arguments.callee.name; },
surname : function surname(){ return arguments.callee.name; },
position : function position(){ return arguments.callee.name; },
}
should do what you say.
EDIT
Banzaaai~ !
var types = {
string: function(){
eval('var x = function '+arguments.callee.caller.name+'(){var x = function(){return arguments.callee.caller.name;}; return x();}');
return x();
}
};
var modelPerson = {
title: function title(){ return types.string(); },
firstName: function firstName(){ return types.string(); },
surname: function surname(){ return types.string(); },
position: function position(){ return types.string(); }
};
SRSLY THOUGH
var types = {
string: function(x){
return function(){ return x; }
}
};
var modelPerson = {
title: types.string('title'),
firstName: types.string('firstName'),
surname: types.string('surname'),
position: types.string('position')
};
Upvotes: 3