ES6 (inspired by sendy halim's answer below):
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.
ES5 (inspired by Vlad's answer):
If you have a reference to the function, you can do:
function functionName( func )
// - ^ the beginning of the string
// - function the word 'function'
// - \s+ at least some white space
// - \s* optionally followed by white space (in theory there won't be any here,
// so if performance is an issue this can be omitted
// - \( 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
callee are considered deprecated.
 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.
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.