Question

[Solved] Get function name in JavaScript

Does anyone know if there is a way to get JavaScript function name. For example I got a function like

function test1(){
alert(1);
}

I have it in my head section. Then I create an object obj1 and put my function there

obj1.func = test1;

When I call a method in obj1 object, do I have any way to get my function name (test1) inside of this method, except parsing the source (this.func.toString()) of the function.

Enquirer: kalan

||

Solution #1:

function test() {  alert(arguments.callee.name); } 
b = test; 
b();

outputs “test” (in Chrome, Firefox and probably Safari). However, arguments.callee.name is only available from inside the function.

If you want to get name from outside you may parse it out of:

b.toString();

but I think name property of function object might be what you need:

alert(b.name);

this however does not seem work for IE and Opera so you are left with parsing it out manually in those browsers.

Respondent: Kamil Szot

Solution #2:

Until ES2015, there was no standard way to get the name of a function. Most current browsers support a name property on Function objects that was non-standard until ES2015, but no current version of IE does. The only option this leaves you if you need to support IE is trying to parse the name from the function’s string representation, which is not a good idea. There’s a (long) discussion here about it: http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/b85dfb2f2006c9f0

Respondent: Tim Down

Solution #3:

The best thing to do is:

function functionName(fun) {
  var ret = fun.toString();
  ret = ret.substr('function '.length);
  ret = ret.substr(0, ret.indexOf('('));
  return ret;
}

Note: Using Function.caller is non-standard and arguments.callee is forbidden in strict mode.

Respondent: Vlad A. Ionescu

Solution #4:

Here’s what I use to put class names in error messages. It includes code to get the name of functions, which works in most browsers.

Obviously, there is no standard way that always works, so you should always provide a name that can be used if no other name is found.

var nameFromToStringRegex = /^functions?([^s(]*)/;

/**
 * Gets the classname of an object or function if it can.  Otherwise returns the provided default.
 *
 * Getting the name of a function is not a standard feature, so while this will work in many
 * cases, it should not be relied upon except for informational messages (e.g. logging and Error
 * messages).
 *
 * @private
 */
function className(object, defaultName) {
    var result = "";
    if (typeof object === 'function') {
        result = object.name || object.toString().match(nameFromToStringRegex)[1];
    } else if (typeof object.constructor === 'function') {
        result = className(object.constructor, defaultName);
    }
    return result || defaultName;
}
Respondent: kybernetikos

Solution #5:

One interesting way I’m experimenting with is a declaration like the following:

var test1 = function test1(){
    alert(1);
};

It’s a little hacky, but what ends up happening is test1 is a local variable that holds a [Function: test1] object.

Here’s what happens when you use code based on it:

test1(); //runs the function as expected
console.log(test1.name); //prints 'test1'

So if you do the following:

obj1.func = test1;

You’ll then be able to reference obj1.func.name and it’ll return 'test1'.

Respondent: jdotjdot

Solution #6:

This is probably the best way to do it:

var myfunc = function () {};
var funcName = myfunc.constructor.name;

This can be done outside the execution of the function, and you can check within the context of the browser console.

Happy coding!

Respondent: imalvinz

Solution #7:

You could convert your function into a string (fn + '') and split it later at whitespace and open bracket /s|(/.

var getFunctionName = function (fn) {
   return (fn + '').split(/s|(/)[1];
};
Respondent: yckart

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy