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.
-
4Why do you need the function name?Felix Kling– Felix Kling2010年07月05日 10:54:35 +00:00Commented Jul 5, 2010 at 10:54
7 Answers 7
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.
11 Comments
callee was deprecated as a property of Function.arguments, but retained as a property of arguments: developer.mozilla.org/en/Core_JavaScript_1.5_Reference/… arguments.callee.name wouldn't work in IE 5.5+. I tested it the code in IE 6. Indeed, it doesn't work. I then tested jsfiddle.net/guxRA/2 and it appears that IE 6 produces the source code for the function as arguments.callee. How funny.name property of Function objects is not supported in IE.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
9 Comments
name property is now standardized in ES2015. However, IE still doesn't support name so it's still relevant.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.
1 Comment
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 = /^function\s?([^\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;
}
2 Comments
/* ReferenceError: object_someMethod is not defined */ var Enforcer = function () { try { object_someMethod; } catch (e) { console.log(e); } }; Enforcer.query = function () { try { object_someMethod; } catch (e) { console.log(e); } }/* ReferenceError: object_someMethod is not defined */ var Enforcer = function Enforcer() { try { object_someMethod; } catch (e) { console.log(e); } }; Enforcer.query = function () { try { object_someMethod; } catch (e) { console.log(e); } }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'.
Comments
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!
1 Comment
funcName outputs "Function"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];
};
1 Comment
// ReferenceError: object_someMethod is not defined var Enforcer = function () { try { object_someMethod; } catch (e) { console.log(e); } }; Enforcer.query = function () { try { object_someMethod; } catch (e) { console.log(e); } }