Search code examples
javascriptfunctiontypeof

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


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 Outer(){

    alert(typeof this);

}

Solution

  • I think that you can do that :

    var name = arguments.callee.toString();
    

    For more information on this, take a look at 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();
    }