我可以在JavaScript中获取当前正在运行的函数的名称吗? [英] Can I get the name of the currently running function in JavaScript?

查看:133
本文介绍了我可以在JavaScript中获取当前正在运行的函数的名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以这样做:

myfile.js:
function foo() {
    alert(<my-function-name>);
    // pops-up "foo"
    // or even better: "myfile.js : foo"
}

我的堆栈中有Dojo和jQuery框架,所以如果其中任何一个使它们变得更容易,可以使用它们。

I've got the Dojo and jQuery frameworks in my stack, so if either of those make it easier, they're available.

推荐答案

您应该可以使用 arguments.callee 获取它。

You should be able to get it by using arguments.callee.

你可能必须解析出这个名字,因为它可能会包含一些额外的垃圾。虽然,在某些实现中,您可以使用 arguments.callee.name 获取名称。

You may have to parse out the name though, as it will probably include some extra junk. Though, in some implementations you can simply get the name using arguments.callee.name.

解析: p>

Parsing:

function DisplayMyName() 
{
   var myName = arguments.callee.toString();
   myName = myName.substr('function '.length);
   myName = myName.substr(0, myName.indexOf('('));

   alert(myName);
}




资料来源: Javascript - 获取当前的函数名称

这篇关于我可以在JavaScript中获取当前正在运行的函数的名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆