Get the name of the function variable has been assigned

I am trying to return the name of the variable to which the function is assigned.

I gave an example below. The end result, I would like to modelPerson.title()return the name of the variable title.

For example, I have the following code:

Definition of basic model types

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

Using model types

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

Attempt to return the title

console.log(modelPerson.title());

Sorry if this is a bit unclear. I have included JSFiddle if this helps: http://jsfiddle.net/4f6VE/

Thanks for any help you can give.

+4
source share
5 answers

This is indeed possible, but includes some specific things about 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 should probably use something less insane

+3

, ,

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

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

,

, . , (, , ).

+1

, 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"
0

, ( .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"
0

Source: https://habr.com/ru/post/1526064/


All Articles