You can use toString() to find out if a function is anonymous if it is declared as a named function and not an unnamed function assigned to a variable:
function jim () { var h = "hello"; } function jeff(func) { var fName; var inFunc = func.toString(); var rExp = /^function ([^\s]+) \(\)/; if (fName = inFunc.match(rExp)) fName = fName[1]; alert(fName); }
Gives you the name of the function, if any.
jeff(function () { blah(); }); // alert: null; jeff(function joe () { blah(); }); // alert: "joe"; jeff(jack); // "jack" if jack is function jack () { }, null if jack = function() {}
My previous edit referred to IE quirk, which was not in other browsers, and no longer works in IE since version 9. However, you can still assign named functions as properties of an object using a named function expression:
var obj = { fn: function namedFunction () { } };
This works in all browsers, but IE 8 and below do not adhere to the specification, which says that a function is available only by that name inside its own block.
source share