Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 932
pencilCake Avatar asked Jan 29 '10 10:01

pencilCake


People also ask

How do you call a function name my function in JavaScript?

Define a function named "myFunction", and make it display "Hello World!" in the <p> element. Hint: Use the function keyword to define the function (followed by a name, followed by parentheses). Place the code you want executed by the function, inside curly brackets. Then, call the function.

What is named function in JavaScript?

Named Functions: In JavaScript, named functions are simply a way of referring to a function that employs the function keyword followed by a name that can be used as a callback to that function. Normal functions with a name or identifier are known as named functions.


4 Answers

This will work:

function test() {
  var z = arguments.callee.name;
  console.log(z);
}
like image 107
yannis Avatar answered Oct 09 '22 06:10

yannis


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();
}
like image 25
marcgg Avatar answered Oct 09 '22 07:10

marcgg


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);
};
like image 20
jabacchetta Avatar answered Oct 09 '22 06:10

jabacchetta


Took me a while to figure this out, so tried to make it very clear for rookies like me.

Approach 1 - arguments.callee.name

This approach used to work, but now in ES6 Strict Mode it will fail. Don't use Approach 1.

//approach 1 - don't use
let functionName = arguments.callee.name;
console.log(functionName);

Approach 2 - create a function and use caller.name

This approach works in the latest version of Javascript and will not fail in Strict Mode. Use Approach 2. There are 2 steps.

Step 1 - Create a function that uses caller.name to return the name of the function that called it. Add this function to your code:

function getFuncName() {
   return getFuncName.caller.name
}

Step 2 - Call your function when you need the name of the function your code is currently in.

    function iWantThisName() { 
      console.log(getFuncName())
    }
    
    iWantThisName() 
    // Logs: "iWantThisName"
like image 45
Joshua Dance Avatar answered Oct 09 '22 07:10

Joshua Dance