I want to do the opposite of Get JavaScript function-object from its name as a string?
That is, given:
function foo()
{}
function bar(callback)
{
var name = ???; // how to get "foo" from callback?
}
bar(foo);
How do I get the name of the function behind a reference?
9 Answers 9
If you can't use myFunction.name then you can:
// Add a new method available on all function values
Function.prototype.getName = function(){
// Find zero or more non-paren chars after the function start
return /function ([^(]*)/.exec( this+"" )[1];
};
Or for modern browsers that don't support the name property (do they exist?) add it directly:
if (Function.prototype.name === undefined){
// Add a custom property to all function values
// that actually invokes a method to get the value
Object.defineProperty(Function.prototype,'name',{
get:function(){
return /function ([^(]*)/.exec( this+"" )[1];
}
});
}
5 Comments
( if for the exec match, not a part of the regex. cool. +1. BTW, why it doesn't alert foo)?foo){...?function in the regex starts it matching, and then it consumes zero or more anything-but-a-( characters (as many as possible). Once it has done that, it must stop; it can't "skip over" non-matches and continue on.var name = callback.name;
MDN:
The name property returns the name of a function, or an empty string for anonymous functions:
1 Comment
[the name] property is not standard is no longer true, it's in ECMA 6.0 / 2015: ecma-international.org/ecma-262/6.0/#sec-setfunctionname / Also see 2ality.com/2015/09/function-names-es6.html function bar(callback){
var name=callback.toString();
var reg=/function ([^\(]*)/;
return reg.exec(name)[1];
}
>>> function foo() { };
>>> bar(foo);
"foo"
>>> bar(function(){});
""
3 Comments
function?bar could easily be adjusted to take that into account.var x = function fooBar(){};
console.log(x.name);
// "fooBar"
Comments
try to access the .name property:
callback.name
Comments
You can extract the object and function name with:
function getFunctionName()
{
return (new Error()).stack.split('\n')[2].split(' ')[5];
}
For example:
function MyObject()
{
}
MyObject.prototype.hi = function hi()
{
console.log(getFunctionName());
};
var myObject = new MyObject();
myObject.hi(); // outputs "MyObject.hi"
3 Comments
this.toString()?If you were looking for the function on an specific object event, this may help:
var a = document.form1
a.onsubmit.name
Comments
for me, with just a little modification (adding \ before parent), this work:
if (Function.prototype.name === undefined){
// Add a custom property to all function values
// that actually invokes a method to get the value
Object.defineProperty(Function.prototype,'name',{
get:function(){
return /function ([^\(]*)/.exec( this+"" )[1];
}
});
}
Comments
I think the answer is simpler than most of these. If the function is
function func() {}
then the name of the function as a string is func.toString() .
If the function is unnamed, then you would have to find the name of the variable or constant that stores its name (which depends on the program code).
var bat = foo; bar(bat)- now what should it print?foo. that's the function name.batis a variable with a reference to thefoofunction.bar(function() {...});