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?

2
  • Not a duplicate, that question includes frameworks, mine EXPLICITLY forbids it. Commented Jun 4, 2013 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. Commented Jun 4, 2013 at 20:03

2 Answers 2

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
} 
Sign up to request clarification or add additional context in comments.

3 Comments

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?
this is correct, but should be noted that it is deprecated in ECMAScript5 strict mode and may not be supported going forward. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
You mean, like this? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… . 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).
0

You can always ask for a functions name :)

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

tellName(console.log);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.