How can I get the name of function inside a JavaScript function?

后端 未结 3 977
暖寄归人
暖寄归人 2020-12-13 09:12

How is it possible to learn the name of function I am in?

The below code alerts \'Object\'. But I need to know how to alert \"Outer.\"

function Oute         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 10:08

    As of ES6, you can use Function.prototype.name. This has the added benefit of working with arrow functions, since they do not have their own arguments object.

    function logFuncName() {
      console.log(logFuncName.name);
    }
    
    const logFuncName2 = () => {
      console.log(logFuncName2.name);
    };
    

提交回复
热议问题