You can use toString()
to find out if the function is anonymous assuming it is declared as a named function and not an unnamed function assigned to a variable:
See updated code below for an improved regex for IE
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);
}
Will give 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() {}
EDIT
Lewis clarified the question for me and his answer is correct to the extent that functions declared as object properties are unnamed, but this answer does state that only named functions work and you can still declare an object property via a named function:
var jim = { };
function jeff(func)
{
var fName;
var inFunc = func.toString();
var rExp = /^function ([^\s(]+)\s?\(\)/;
if (fName = inFunc.match(rExp))
fName = fName[1];
window.prompt(fName, inFunc);
}
(function () { function jim.jack () {} })();
jeff(jim.jack);
The downside to this is that named functions that are properties of objects must be within a child function of the current scope because named functions are set at parse time as opposed to unnamed functions which are set at execution time:
var jim = {};
function jim.jack () {} // error
(function () { function jim.jack () {} })(); // no error