【发布时间】:2015-01-15 14:29:16
【问题描述】:
考虑下一个例子:
function Test(){
Log();
}
function Log(){
console.log('Name of the called function');
}
我如何获取函数的名称,在本例中为 Test(),在没有 corse 参数的情况下进入我的 Log 函数。
考虑这种方式是可行的:
function Hello(){
Log(); //should return Hello
}
function Test(){
Log(); //should return Test
}
function World(){
Log(); //should return World
}
function Log(){
return NAME_OF_FUNCTION;
}
谢谢
更新:
我可以使用 arguments.callee.name 来获取名称,但是,这在严格模式下不起作用,如果我删除 de strict 模式并尝试使用它,我会得到 clousure 函数的名称而不是父方法..例如:
var Log = (function(){
function info(message){
console.log(arguments.callee) //This print function info(message){
}
return {
info:info
}
})();
function Test(){
Log.info("Some Message");
}
这是错误的,我需要的是 Test 函数的名称,而不是 info 函数。
【问题讨论】:
标签: javascript