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
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.
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).
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
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')
};
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With