JavaScript
Article
By Sam Deering

jQuery console.log Current Function Name

By Sam Deering
Help us help you! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

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…

--ADVERTISEMENT--

Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?