javascript函数是否知道其名称 [英] Does javascript function know its name

查看:50
本文介绍了javascript函数是否知道其名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为getItem的函数.我想使用其中的代码来读取此函数的名称.这可能吗?

I have a function called getItem. I want to read the name of this function using code from within it. Is this possible?

function getItem(){ 
   var functionName =   //how do I read the function name;
   alert(functionName)  //outputs 'getItem'
}

推荐答案

尝试一下:

function getItem(){ 
   var functionName =   arguments.callee.name;
   alert(functionName)  //outputs 'getItem'
}

这是小提琴: http://jsfiddle.net/maniator/xGzKA/

另请参见以前的堆栈Q 寻求另一种解决方案

also see this previous stack Q for another solution

这篇关于javascript函数是否知道其名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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