1

In PHP it is possible to pass the name of a function to another function...

function fail()
{
 $query1 = 'SELECT null FROM null;';
 $result1 = mysql_query($query1);

 if ($result1) {echo 'Reverse-hippies in the code.';}
 else {mysql_error_report($query1,mysql_error(),__FUNCTION__);}
}

PHP's __FUNCTION__ magic constant is dynamic, that means when I setup error reporting for any/all queries I don't have to manually copy/paste the name of the function (that would be static); this is especially useful when changing the name of the functions.

Does JavaScript (NOT any frameworks! and not Firebug/other JavaScript debuggers) have this same dynamic functionality built in even in later iterations?

John
  • 1
  • 13
  • 98
  • 177
  • Not a duplicate, that question includes frameworks, mine EXPLICITLY forbids it. – John Jun 04 '13 at 19:54
  • 1
    @John the top answer in that question doesn't involve frameworks though. That said, the answer given there is deprecated in ES5 strict mode so it may be worth asking if anyone has newer answers. – Ben McCormick Jun 04 '13 at 20:03

2 Answers2

3
arguments.callee.name

is what you are looking for. this will return the name of the function you are in.

function foo () {
    console.log(arguments.callee.name); //foo
} 
recneps
  • 1,285
  • 5
  • 16
  • 27
  • That looks like Firebug, so if it's not already built in to the language itself then it doesn't count. If it IS then please refer to the place in a spec you found it? – John Jun 04 '13 at 19:55
  • this is correct, but should be noted that it is deprecated in ECMAScript5 strict mode and may not be supported going forward. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments/callee – Ben McCormick Jun 04 '13 at 20:01
  • You mean, like this? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments/callee?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FFunctions_and_function_scope%2Farguments%2Fcallee . It's sort of deprecated and does not work in some IE browsers, but I think your only other option is to parse the function name (That's just a little too dirty for me). – recneps Jun 04 '13 at 20:02
0

You can always ask for a functions name :)

function tellName(f) {
    console.log(f.name);
}

tellName(console.log);
mzedeler
  • 4,177
  • 4
  • 28
  • 41