命名匿名功能 [英] Naming an anonymous function
问题描述
有可能以某种方式设置匿名函数的名称?
Is it possible to somehow set a name for anonymous functions?
无需为名称空间添加函数名称,但我想避免在我的javascript调试器中看到大量的(?),所以我可以保持调用堆栈跟踪信息。
There is no need to add function names to the namespace for anonymous functions but I would like to avoid seeing a large amount of (?) in my javascript debugger so I can keep the call stack trace informative.
我还可以安全地将正常声明的函数传递为参数而不是匿名功能或我会走进一些奇怪的错误。它似乎工作。
Also can I safely pass normal declared functions as arguments instead of anonymous functions or will I walk into some strange errors. It seems to work.
$("object").bind("click", function() { alert("x"); });
$("object").bind("click", function debuggingName() { alert("x"); });
我的意思是沿着
$("object").bind("click", function() { Function.Name = "debuggingName"; alert("x"); });
推荐答案
您的第二个例子是使用命名函数表达式,在大多数浏览器中都可以正常工作,但是在使用IE之前应该注意IE中有一些问题。我建议您阅读 kangax关于此主题的优秀文章。
Your second example is using a named function expression, which works fine in most browsers but has some problems in IE that you should be aware of before using it. I recommend reading kangax's excellent article on this subject.
这篇关于命名匿名功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!