How to get function name using object inside function in javascript?

In this post, You learn how to get function names from inside a function and outside a function.

The function can be created with a name called named functions as seen below.

function namedfunction() {
  // code inside the function
}

Also, functions created without a name are called anonymous functions (without a name)

var anonymousFunction = function () {
  // code inside the function
};

ES6 way to get the function name

We can get the function name using

  • function.name
  • object.constructor.name

function name property to get the function name

ECMAScript 6 introduction Function object which has name attribute to give the name of the function.

It is readonly property.

Let’s see how to get named functions.

function myfunction() {
  console.log("my function");
}
console.log(myfunction.name); //myfunction
console.log(myfunction.prototype.constructor.name); //myfunction

constructor.name property

We can also get function names using object

Let’s create an object of function.

The object has a constructor object which has a name property. This can also be invoked with the class prototype as seen below.

Here is a syntax

{function}.prototype.constructor.name
{object}.constructor.name

Here is a code to `for example get the name of the named function.

function myfunction1() {
  console.log("myfunction1"); //Person
}
var mf = new myfunction1();
console.log(myfunction1.prototype.constructor.name); //myfunction1
console.log(mf.constructor.name); //myfunction1

Here is a code for example to get the name of anonymous function

const myfunction = function () {
  console.log("my function");
};
console.log(myfunction.name); //myfunction
console.log(myfunction.prototype.constructor.name); //myfunction

How to get the running function name

We have a function in javascript running, and want to get the name of the function inside a body.

we can get the name of the function in many ways

  • using arguments callee

arguments.callee returns the function definition, and from that definition, gets the function name using string functions.

Here is a complete code example

function myfunction() {
  var functName = arguments.callee.toString();
  console.log(functName); // prints function definition
  functName = functName.substr("function ".length);
  functName = functName.substr(0, functName.indexOf("("));
  console.log(functName);
}
myfunction();

Another way is to get arguments.callee.name

In this example, the Function is defined and called from the outside global scope.

function myfunction1() {
  console.log(arguments.callee.name);
}
myfunction1();

How to get dynamic function names in javascript

As dynamic functions are created with different names dynamically.

Let’s create a dynamic function with a variable containing the name of the function.

Inside a function, you can access the name directly that returns the name of the function.

var dynamicfunction = "myfunction";

window[dynamicfunction] = function () {
  console.log(dynamicfunction);
};

Conclusion

You learned multiple ways to find the name of the function inside a running function and from the global scope outside.