How to get the function name from within that function?

How to get the function name from within that function?

How can I access a function name from inside that function?
// parasitic inheritance
var ns.parent.child = function() {
var parent = new ns.parent();
parent.newFunc = function() {

}
return parent;
}

var ns.parent = function() {
// at this point, i want to know who the child is that called the parent
// ie
}

var obj = new ns.parent.child();

Solutions/Answers:

Solution 1:

In ES5, 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;
}

Using Function.caller is non-standard. Function.caller and arguments.callee are both forbidden in strict mode.

Edit: nus’s regex based answer below achieves the same thing, but has better performance!

In ES6, you can just use myFunction.name.

Note: Beware that some JS minifiers might throw away function names, to compress better; you may need to tweak their settings to avoid that.

Solution 2:

ES6 (inspired by sendy halim’s answer below):

myFunction.name

Explanation on MDN. As of 2015 works in nodejs and all major browsers except IE.

Note: On bound functions this will give “bound <originalName>“. You will have to strip the “bound ” if you want to get the original name.

Related:  What is SpreadElement in ECMAScript documentation? Is it the same as Spread syntax at MDN?

ES5 (inspired by Vlad’s answer):

If you have a reference to the function, you can do:

function functionName( func )
{
    // Match:
    // - ^          the beginning of the string
    // - function   the word 'function'
    // - \s+        at least some white space
    // - ([\w\$]+)  capture one or more valid JavaScript identifier characters
    // - \s*        optionally followed by white space (in theory there won't be any here,
    //              so if performance is an issue this can be omitted[1]
    // - \(         followed by an opening brace
    //
    var result = /^function\s+([\w\$]+)\s*\(/.exec( func.toString() )

    return  result  ?  result[ 1 ]  :  '' // for an anonymous function there won't be a match
}
  • I have not run unit tests on this, or verified implementation
    differences, but in principle it should work, if not leave a comment.
  • Note: won’t work on bound functions
  • Note: that caller and callee are considered deprecated.

[1] I include it here because it is legal and often enough syntax highlighting tools fail to take into account the white space between function name and parenthesis. On the other hand, I’m not aware of any implementation of .toString() that will include white space here, so that’s why you can omit it.


As an answer to the original question, I would drop parasitic inheritance and go for some more traditional OOP design patterns. I wrote a TidBits.OoJs to comfortably write OOP code in JavaScript with a feature set mimicking C++ (not yet complete, but mostly).

Related:  What does Dart's output look like?

I see from the comments that you would like to avoid passing information parent needs to it’s constructor. I must admit that traditional design patterns won’t save you from that one though, since it is generally a considered a good thing to make your dependencies obvious and enforced.

I would also suggest to steer away from anonymous functions. They only make debugging and profiling a PITA because everything just shows up as “anonymous function”, and there is no benefit to them that I’m aware of.

Solution 3:

what you’re doing is assigning unnamed function to a variable. you probably need named function expression instead ( http://kangax.github.com/nfe/ ).

var x = function x() {
    console.log( arguments.callee.name );
}
x();

however I’m not sure how much cross-browser that is; there’s an issue with IE6 that makes you function’s name leak to the outer scope. also, arguments.callee is kind of deprecated and will result in error if you’re using strict mode.

Solution 4:

Any constructor exposes a property name, which is the function name. You access the constructor via an instance (using new) or a prototype:

function Person() {
  console.log(this.constructor.name); //Person
}

var p = new Person();
console.log(p.constructor.name); //Person

console.log(Person.prototype.constructor.name);  //Person

Solution 5:

This might work for you:

function foo() { bar(); }

function bar() { console.log(bar.caller.name); }

running foo() will output “foo” or undefined if you call from an anonymous function.

Related:  How to force update Single Page Application (SPA) pages?

It works with constructors too, in which case it would output the name of the calling constructor (eg “Foo”).

More info here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Caller

They claim it’s non-standard, but also that it’s supported by all major browsers: Firefox, Safari, Chrome, Opera and IE.

Solution 6:

You can’t. Functions don’t have names according to the standard (though mozilla has such an attribute) – they can only be assigned to variables with names.

Also your comment:

// access fully qualified name (ie "my.namespace.myFunc")

is inside the function my.namespace.myFunc.getFn

What you can do is return the constructor of an object created by new

So you could say

var obj = new my.namespace.myFunc();
console.info(obj.constructor); //my.namespace.myFunc