定义确定关联实例的本地变量名称的方法

时间:2016-02-29 21:38:21

标签: javascript

假设我有这个功能:

function myFunction(x,y,z) {
   this.var1 = x*y;
   this.var2 = "someting else";
};

myFunction.prototype.whatismyname = function() {
 // return my name here.
};

var varName = new myFunction(10,99,7);

我想致电varName.whatismyname();并让它返回文字 "varName",或者声明的变量的名称。也就是说,我可以使用一个方法来为我提供用于声明实例变量的名称吗?

1 个答案:

答案 0 :(得分:1)

这实际上是不可能的。在JavaScript中,本地变量名称不以任何语义方式保留;该语言没有可以让您检索它们的反射功能。假设在正确的环境中连接到自己的调试器端口以确定本地变量名称,这可能是假设可能的,但这将非常复杂和缓慢。

但作为一项有趣的练习,这里有一个功能,可以为您提供的具体示例工作请不要在真实代码中使用此功能。此代码检查您的类的名称(this.constructor.name),然后在调用堆栈中搜索包含X = new NAME的任何父函数,并返回它找到的第一个X。这种方法极其粗糙且不可靠,依赖于超级弃用的.caller属性,可能会损害您的应用程序性能,无法在strict模式下工作,您的同事会给您带来肮脏的外观



function myFunction(x,y,z) {
  this.var1 = x*y;
  this.var2 = "someting else";
};

myFunction.prototype.whatismyname = function() {
  // Get the name of this class's constructor.
  var name = this.constructor.name;

  // Start by searching in the function calling this method.
  var caller = arguments.callee.caller;  
 
  var match = null;

  // Search up the call stack until we find a match or give up.
  while (!match && caller) {
    // Get the source code of this function on the stack.
    var code = caller.toString();

    // Search the source code for X = new NAME.
    var match = new RegExp('\\W(\\w+)\\s*=\\s*new\\s+' + name).exec(code);

    // Move up the stack.
    caller = caller.caller;
  }

  // Return the first match.
  return match && match[1] || undefined;
};

function main() {
  var varName = new myFunction(10,99,7);
  other(varName);
}

function other(myObj) {
  console.log(myObj.whatismyname()); // "varName"
}

main();




它"工作"!有点。在这种情况下。不要这样做。

相关问题