简体   繁体   English

是否有可能获得匿名JavaScript函数的名称?

[英]Is it possible to get the name of an anonymous JavaScript function?

Is it possible to get the name of an anonymous function that declared as follows? 是否有可能获得声明如下的匿名函数的名称?

var F = function(){};

At first sight, the answer is no, but apparently the browsers know and hold the function name: 乍一看,答案是否定的,但显然浏览器知道并保留了函数名称:

var F = function(){};
var x = new F();
console.log(x.constructor);   // function F()

(Firefox) (火狐)

var F = function(){};
var x = new F();
console.log(x);    // F {}

(Chrome) (铬)

Is this name somehow accessible? 这个名字是否可以访问? I need it mainly for error logging, so the solution doesn't have to be cross-browser. 我需要它主要用于错误记录,因此解决方案不必是跨浏览器。

Edit for clarification: 编辑以澄清:

I'm getting an objects from external code that I need to know their types, therefore obvious answers like using another declaration ways are not what I'm searching. 我从外部代码中获取了一个我需要知道其类型的对象,因此使用其他声明方式的明显答案不是我正在搜索的内容。

In the use-case that you're trying, it's possible that there can be more than one variable holding the same anonymous function. 在您正在尝试的用例中,可能存在多个具有相同匿名函数的变量。

ie, it can go on like: 即,它可以继续下去:

var F = function(){};
var x = new F();
var x2 = x;
var x3 = x;
var blah = x3;

So, now we have more than one name to look for. 所以,现在我们有多个名字可供查找。 And the first thing I thought of is to loop through all the objects under window and print their name which is having the same method as the value. 我想到的第一件事是遍历窗口下的所有对象并打印它们的名称,该名称与值具有相同的方法。

So, you'd think of something like: 所以,你会想到这样的事情:

for each (item in window){
   if (myfunc == item){
      console.log(/*Somehow print the actual name of the item */)
   }
}

But, it doesn't work that way since, now item is another variable, and look like there is no built-in property that gives the variable name. 但是,它不起作用,因为现在item是另一个变量,看起来没有提供变量名称的内置属性。 May be, have a look at Variable name as a string in Javascript , not that it helps here, though... 可能是,看一下变量名称作为Javascript中的字符串 ,不是它在这里有帮助,尽管......

So, finally, since you mentioned you're trying to do error logging, thought of using stack-traces. 所以,最后,因为你提到你正在尝试进行错误记录,所以想到使用堆栈跟踪。 Don't know if it will apply in your situation, may be still a bit enlightening :) 不知道它是否适用于你的情况,可能还是有点启发:)

And this is how it goes: (To be warned, this is a hack) 这就是它的方式:(要注意,这是一个黑客攻击)

var myFunction = function(){   
  var err = new Error();   
  console.log(err.stack) 
} 
myFunction();

will output something like: 将输出如下内容:

myFunction@debugger eval code:3:17
@debugger eval code:6:5
@debugger eval code:1:15

And, taking that to the next level: 而且,把它提升到一个新的水平:

var myFunction = function(){   
  var err = new Error();   
  console.log(err.stack.split("@")[0]) 
 /* split the stack-trace string and get the first word before @ */
} 
myFunction();

Now, the output for that will be: 现在,输出将是:

myFunction

Which, indeed is the name of the variable that is holding the anonymous function. 其中,确实是持有匿名函数的变量的名称。

Note: This answer is inspired by the question How can I get a Javascript stack trace when I throw an exception? 注意:这个答案的灵感来自于如何在抛出异常时获取Javascript堆栈跟踪的问题

Have tried this only in Firefox, there's a chance the stack-trace might be different elsewhere. 仅在Firefox中尝试过此操作,堆栈跟踪可能在其他地方有所不同。

Edit: Failed to notice your edit, this might need editing of declarations, which breaks your use-case. 编辑:无法注意到您的编辑,这可能需要编辑声明,这会破坏您的用例。 :( :(

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

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