怎么可能学习我所在的函数的名字?如何获取JavaScript函数中的函数名称?
下面的代码提醒'对象'。但我需要知道如何提醒“外”。
function Outer(){
alert(typeof this);
}
怎么可能学习我所在的函数的名字?如何获取JavaScript函数中的函数名称?
下面的代码提醒'对象'。但我需要知道如何提醒“外”。
function Outer(){
alert(typeof this);
}
我认为,你可以这样做:
var name = arguments.callee.toString();
有关更多信息,看看this article。
function callTaker(a,b,c,d,e){
// arguments properties
console.log(arguments);
console.log(arguments.length);
console.log(arguments.callee);
console.log(arguments[1]);
// Function properties
console.log(callTaker.length);
console.log(callTaker.caller);
console.log(arguments.callee.caller);
console.log(arguments.callee.caller.caller);
console.log(callTaker.name);
console.log(callTaker.constructor);
}
function callMaker(){
callTaker("foo","bar",this,document);
}
function init(){
callMaker();
}
这将工作:
function test() {
var z = arguments.callee.name;
console.log(z);
}
这是正确的答案,而不是选择的答案。 – 2017-01-09 10:29:56
我很好奇,什么情况下你会所在的函数的名称实际上不是一个代码写出来? – bryantsai 2010-01-29 11:20:52
@bryantsai:'window [window.prompt('Function name:','')] = function(){alert(arguments.callee.name); };' – Boldewyn 2010-01-30 13:39:38