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


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

Function can be created with name called named functions as seen below

function namedfunction() {
    // code inside function
}

Also, Function created without name called anonymous functions (without name)

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

ES6 way to get function name

We can get function name using

  • function.name
  • object.constructor.name

function name property

ECMAScript 6 introduction Function object which has name attribute to give 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 name using object

Let’s create a object of function.

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

Here is an syntax

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

Here is an code to example get name of 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 an code for example to get 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 running function name

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

we can get name of the function in many ways

  • using arguments callee

arguments.callee returns function defination, from that defination, get function name using string functions.

Here is an complete code example

function myfunction() {
        var functName = arguments.callee.toString();
        console.log(functName); // prints function defination
        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, Function is defined and called from 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 variable contains name of the function

Inside a function, you can access the name directly which 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 global scope outside

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.