JavaScript
Article

jQuery console.log Current Function Name

By Sam Deering

Here is some quick JavaScript code snippets to find out and log the current function name in JavaScript.

//way 1
var fName = arguments.callee.toString();
console.log(fName);
fName = fName.substr('function '.length);
console.log(fName);
fName = fName.substr(0, fName.indexOf('('));
console.log(fName);

//way 2
var fName = arguments.callee.toString().match(/functions+([^s(]+)/);
console.log(fName);
console.log(fName[1]);

//way 3
var fName = arguments.callee.toString(),
    regEx = /^functions+([^(]+)/
console.log(regEx.exec(fName)[1]);

Does anyone know of a way to get the function name for a object literal syntax function?? I don’t think it’s possible…

No Reader comments

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.