首页 > 其他 > 详细

arguments.callee

时间:2019-12-13 14:30:43      阅读:114      评论:0      收藏:0      [点我收藏+]

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);
};

arguments.callee

原文:https://www.cnblogs.com/xsSystem/p/12034834.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!