簡體   English   中英

獲取分配給變量函數的名稱

[英]Get the name of variable function was assigned to

我正在嘗試返回分配函數的變量的名稱。

我在下面列舉了一個例子。 最終結果是我希望modelPerson.title()返回變量名稱title

例如,我有以下代碼:

定義一些基本模型類型

var types = {
    string: function() {
        return function() {
            return "I want this to return 'title'";
        }
    }
};

使用模型類型

var modelPerson = {
    title: types.string(),
    firstName: types.string(),
    surname: types.string(),
    position: types.string()
};

試圖返回標題

console.log(modelPerson.title());

對不起,如果這有點不清楚。 我有一個JSFiddle,如果它有幫助: http//jsfiddle.net/4f6VE/

謝謝你提供的所有幫助

這實際上是可能的,但涉及一些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());

但你可能應該使用一些不那么瘋狂的東西

我真的不知道這是為了什么,但是

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; },
}

應該做你說的。

編輯

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

很幸運的

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

我正在嘗試返回分配函數的變量的名稱

你不能,不可靠。 多個變量或屬性可以引用同一個對象,並且某些對象不會被分配給變量(例如沒有立即調用的名稱的函數表達式)。

最終結果是我希望modelPerson.title()返回變量名稱標題。

然后使用這樣的東西:

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"

這是一個可以在嚴格模式下工作的方法(沒有棄用的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