<分区>
我可以使用函数内部的变量获取函数的名称吗?
function abcd() {
var temp;
alert(" name of the function using temp ");
};
如何使用 temp 获取函数 'abcd'
的名称?
<分区>
我可以使用函数内部的变量获取函数的名称吗?
function abcd() {
var temp;
alert(" name of the function using temp ");
};
如何使用 temp 获取函数 'abcd'
的名称?
最佳答案
你可以像这样使用arguments.callee.name
function abcd() {
console.log("name of the function using", arguments.callee.name);
};
abcd();
# name of the function using abcd
注意:arguments.callee.name
仅适用于使用名称定义的函数。它不适用于匿名函数。例如,如果您像这样定义了函数
var abcd = function () {
console.log("name of the function using", arguments.callee.name);
};
abcd()
只会打印使用函数的名称
。因为在这种情况下 arguments.callee.name
将是一个空字符串。
关于javascript - 是否可以通过函数内部声明的变量获取函数名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24755316/