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?
-
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
1 Answer
Reset to default 5You 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
版权声明:本文标题:Is there an equivalent to PHP's `__FUNCTION__` in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745180602a2646447.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论