arguments.callee

var i = 2
function f() {
  console.log(i)
  i = i - 1;
  if (i > 0) arguments.callee()
}
function f2(){
  f()
}
f2()

如何在JavaScript函数中获取函数名称?

怎么可能学习我所在的功能名称? 以下代码警告'对象'。但我需要知道如何警告“外面”。
function Outer(){

    alert(typeof this);

}
    
我认为你可以这样做:
var name = arguments.callee.toString();
有关这方面的更多信息,请查看本文。
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);
}
    

目前的答案已经过时了。从ES6开始,你可以使用

Function.prototype.name

。这具有使用箭头函数的额外好处,因为它们没有自己的参数对象。

function logFuncName() {
  console.log(logFuncName.name);
}

const logFuncName2 = () => {
  console.log(logFuncName2.name);
};
posted on 2019-12-13 14:14  xsSystem  阅读(216)  评论(0编辑  收藏  举报