admin管理员组

文章数量:1415484

I'm looking for an equivalent to PHP's __FUNCTION__ in JavaScript, to allow me to get the name of the current function. For example:

function foo(){
    console.log(__FUNCTION__); // "foo" would be logged to the console
}

Is there a way to do this in JavaScript? Either with a magic variable similar to __FUNCTION__ or any other workaround? And, if there's not a way to currently achieve this, is it planned?

I'm looking for an equivalent to PHP's __FUNCTION__ in JavaScript, to allow me to get the name of the current function. For example:

function foo(){
    console.log(__FUNCTION__); // "foo" would be logged to the console
}

Is there a way to do this in JavaScript? Either with a magic variable similar to __FUNCTION__ or any other workaround? And, if there's not a way to currently achieve this, is it planned?

Share Improve this question asked Oct 10, 2012 at 16:45 0b100110b10011 18.8k4 gold badges66 silver badges89 bronze badges 6
  • 6 A workaround could be arguments.callee.name – Bergi Commented Oct 10, 2012 at 16:47
  • What @Bergi says sounds like the solution. With the limitation that Warning: The 5th edition of ECMAScript forbids use of arguments.callee() in strict mode - but I guess that has implications only if your code opts in to strict mode. – Pekka Commented Oct 10, 2012 at 16:50
  • Another question might be why you need to know the name of the function? Depending on scope, the function name may be registered with the window object, it may be stuck inside a closure ... I'm just curious what use-case would require the name of the function in the first place to operate correctly? (I realize that there may be use-cases, I'm just curious if there might be another way of approaching your problem with a little more context.) – Jason M. Batchelor Commented Oct 10, 2012 at 17:00
  • 1 @mori57 An example could be for the function to modify itself (as a property of an object), i.e. you need to run a test many times that takes a lot of putation, but after you've done it once you know the answer won't change so you want to short-cut to the result. – Paul S. Commented Oct 10, 2012 at 17:17
  • @mori57 It's for a tokenizer (where the state === function name). Specifically, to force the tokenizer to set the state to the current function in cases where it was changed (instead of having to store another variable). I ended up finding a different way of doing things, but I'm really curious to see if there's a way to achieve this for future purposes as well. – 0b10011 Commented Oct 10, 2012 at 19:42
 |  Show 1 more ment

1 Answer 1

Reset to default 5

You might get a reference to the currently executing function via the callee property of the arguments object. Notice that it is deprecated with ES5.1 strict mode.

From that, you can get the (non-standard) name of the function. Notice that this only works for function declarations and named function expressions (with the known bugs in IE), but not for anonymous functions as object properties:

var myObj = {
    method: function() { // unnamed!
        return arguments.callee.name || "anonymous";
    }
};
myObj.method(); // "anonymous"

I for myself use static strings in debugging / error statements, prepending the whole namespace(s) of the function to easily locate it in the code.

本文标签: Is there an equivalent to PHP39s FUNCTION in JavaScriptStack Overflow