简体   繁体   中英

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

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')
};

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).

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"

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM