Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the name of variable function was assigned to

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

like image 793
Mark Avatar asked Feb 09 '14 21:02

Mark


People also ask

Is function name are variable Python?

In Python, functions are "first class'' objects: they can have variable names assigned to them, they can be passed as arguments to other functions, and can even be returned from other functions.

What is VAR function in JavaScript?

The var statement declares a variable. Variables are containers for storing information. Creating a variable in JavaScript is called "declaring" a variable: var carName; After the declaration, the variable is empty (it has no value).


2 Answers

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

like image 115
vkurchatkin Avatar answered Nov 14 '22 22:11

vkurchatkin


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')
};
like image 43
ElDoRado1239 Avatar answered Nov 14 '22 22:11

ElDoRado1239