简体   繁体   English

获取分配给变量函数的名称

[英]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 . 最终结果是我希望modelPerson.title()返回变量名称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/ 我有一个JSFiddle,如果它有帮助: http//jsfiddle.net/4f6VE/

Thanks for any help you can give 谢谢你提供的所有帮助

That's actually possible, but involves some v8 specific stuff: 这实际上是可能的,但涉及一些v8特定的东西:

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~ ! 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. 最终结果是我希望modelPerson.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: 这是一个可以在严格模式下工作的方法(没有弃用的arguments.callee或专有的arguments.callee.caller属性),使用您的代码进行最少的重新分解并且没有硬编码的名称:

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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