JavaScript 中的匿名函数是一种不好的做法吗?
我读到在 javascript 中使用匿名函数是不好的做法,因为它会让调试变得痛苦,但我自己还没有看到这一点。 JavaScript 中的匿名函数真的是不好的做法吗?如果是的话,为什么?
I was reading that using anonymous functions in javascript is bad practice, because it can make debugging a pain, but I haven't seen this for myself. Are anonymous functions in JavaScript really bad practice and, if so, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我将在这里稍微反其道而行之,证明匿名函数确实是不好的做法,尽管它们被广泛使用。
1)匿名函数不能重复使用。
2) 根据定义,匿名函数没有名称,因此不能描述它们的作用。这就是说代码不是自记录的。
3)匿名函数不能单独使用单元测试框架进行测试。
4)我个人认为它们使代码更难以阅读和调试。尽管您的经历可能会有所不同。
我确实认为在某些情况下匿名函数是最好的选择,并且作为一般规则,为了避免上述缺点,我几乎总是命名我的函数。
通常,匿名函数越长,它就越有可能从拥有名称中受益。
I am going to go against the flow a little here and make the case that anonymous functions are indeed bad practice even though they are widely used.
1) Anonymous functions cannot be reused.
2) Anonymous functions, by definition, do not have a name and so do not describe what they do. Which is to say the code is not self documenting.
3) Anonymous functions cannot be tested in isolation with a unit testing framework.
4) I personally think they make code more difficult to read and debug. Though your experience may vary.
I do think there are situations where an anonymous function is the best choice and as a general rule in order to avoid the above downsides I almost always name my functions.
Typically the longer your anonymous function becomes the more likely that it would benefit from having a name.
不,匿名函数在 JavaScript 中随处可见。它可能会使某些地方的调试变得更加困难,但还不足以说不应该使用它们。
例如,JQuery 就广泛使用了它们。
很多时候,您想要在正式声明的函数上使用它们,例如当您想要限制它们的范围时。
Nope, anonymous functions are used all over the place in JavaScript across the web. It may make debugging a little more difficult in spots, but not nearly enough to say that they shouldn't be used.
For example, JQuery makes extensive use of them.
There are a lot of times when you want to use them over formally declared functions, such as when you want to limit their scope.
我想说相反,lambda(别名)使一些表达式更加简洁。例如,如果您将多个事件处理程序绑定到多个事件,那么为每个事件处理程序提供函数名称将是很乏味的。
它比没有更有帮助和节省时间,即使它使调试有点困难,但我很少在调试上遇到困难,因为函数是匿名的。您应该使用 JSLint 来让您的编码变得更轻松。
I would say the contrary, lambdas ( alias ) make some expressions much more succinct. If you're binding multiple event handlers to multiple events it would be tedious giving a function name to each and every event handler, for example.
It's more helpful and time-conserving than not, even if it makes debugging a little bit harder but I rarely struggle with debugging because a function is anonymous. And you should use JSLint to make your life easier when coding.
仅仅因为每个人都使用它们并不意味着它们就是好的实践(每个人都记得使用 table 元素进行布局吗?)。但是,它们很棒,因为它们可以帮助澄清和简化代码,减少出错的机会。
但是,匿名函数不应该太复杂以至于调试变得困难。在这种情况下,也许最好创建一个新函数。
Just because everybody uses them doesn't make them good practice (everybody remember using the table element for layout?). But, they're great because they can help clarify and simplify your code, giving less opportunity for something to go wrong.
But, anonymous functions shouldn't be so complicated that debugging becomes difficult with them. In that case, perhaps it's better to make a new function.
绝对不是,lambda 函数随处可见,几乎无处不在。
Most definitely not, lambda functions are used all over the place, almost ubiquitous.
这是我的浏览器控制台:
假设您正在调试某些东西,并且有一个名为
groupy
的函数名称。您输入它的名称以获取有关它的更多信息。如果该函数已设置为 bad 部分,则您不知道原始声明是什么。但是,如果您定义函数名称(如“好”部分中所示),那么您将始终保留原始函数名称的踪迹。Here is my browser console:
Imagine you are debugging something and you have a function name called
groupy
. You type it's name to get more information on it. If that function has been set as in the bad section, you have no idea of what the original declaration was. If however, you define your function name, as in the Good section, then you will always have a trace of the original function name.