JavaScript
Article
By Sam Deering

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…

--ADVERTISEMENT--

Recommended
Sponsors
The most important and interesting stories in tech. Straight to your inbox, daily. Get Versioning.
Login or Create Account to Comment
Login Create Account