在JavaScript事件代码中,对回调和参数使用匿名函数而不是命名函数有什么好处? [英] What are the benefits to using anonymous functions instead of named functions for callbacks and paramaters in JavaScript event code?

查看:165
本文介绍了在JavaScript事件代码中,对回调和参数使用匿名函数而不是命名函数有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaScript的新手。我理解了语言的许多概念,我一直在继承原型继承模型,我用更多和更多的交互式前端东西来鼓励我的口哨。这是一个有趣的语言,但我总是被回调意粉关闭,这是典型的许多非平凡的交​​互模型。

I'm new-ish to JavaScript. I understand many of the concepts of the language, I've been reading up on the prototype inheritance model, and I'm whetting my whistle with more and more interactive front-end stuff. It's an interesting language, but I'm always a bit turned off by the callback spaghetti that is typical of many non-trivial interaction models.

总是看起来很奇怪的东西对我来说,尽管可读性噩梦是JavaScript嵌套回调的巢,我在很多例子和教程中很少看到的一个事情是使用预定义的命名函数作为回调参数。我是天天的Java程序员,而且舍弃了代码单元的企业-Y名称的刻板印记我喜欢在一种语言中工作的强大的IDE的选择是使用有意义的,如果长,名称可以使代码的意图和意义更清楚,而不使实际生产力更难。那么为什么在编写JavaScript代码时不使用相同的方法呢?

Something that has always seemed strange to me is that in spite of the readability nightmare that is a nest of JavaScript nested callbacks, the one thing that I very rarely see in many examples and tutorials is the use of predefined named functions as callback arguments. I'm a Java programmer by day, and discarding the stereotypical jabs about Enterprise-y names for units of code one of the things I've come to enjoy about working in a language with a strong selection of featureful IDE's is that using meaningful, if long, names can make the intent and meaning of code much clearer without making it more difficult to actually be productive. So why not use the same approach when writing JavaScript code?

给出想法,我可以提出对这个想法的论据,但我的天真和

Giving it thought, I can come up with arguments that are both for and against this idea, but my naivety and newness to the language impairs me from reaching any conclusions as to why this would be good at a technical level.


  • 灵活性。具有回调参数的异步函数可以通过许多不同的代码路径中的一个来达到,并且它可能被迫要写一个命名的函数来解决每一个可能的边缘情况。

  • 速度。它对黑客的心态很重要。

  • 其他人都在做这项工作

  • 文件大小较小,即使规模很小,

  • 更简单的AST?我假设匿名函数是在运行时生成的,所以JIT不会把名字映射到指令,但我只是猜到这一点。

  • 更快的调度?也不知道这一个。再次猜测。

  • Flexibility. An asynchronous function with a callback paramater could be reached by one of many different code paths and it could be harried to have to write a named function to account for every single possible edge case.
  • Speed. It plays heavily in to the hacker mentality. Bolt things on to it until it works.
  • Everyone else is doing it
  • Smaller file sizes, even if trivially so, but every bit counts on the web.
  • Simpler AST? I would assume that anonymous functions are generated at runtime and so the JIT won't muck about with mapping the name to instructions, but I'm just guessing at this point.
  • Quicker dispatching? Not sure about this one either. Guessing again.


  • 当嵌套的螺母深入到一堆回调中时,这增加了混乱(这是公平的,可能意味着你编写构造不好的代码开始

有了这么多的现代浏览器显示能够比以前更快地执行JavaScript代码的能力,我没有看到任何微不足道的性能获得一个可能出来使用匿名回调是必要的。看起来,如果你在使用命名函数是可行的(可预测的行为和执行的路径),那么没有理由不会。

With so many modern browsers showing the ability to execute JavaScript code much faster than before, I'm failing to see how any trivial sort of performance gain one might get out using anonymous callbacks would be a necessity. It seems that, if you are in a situation where using a named function is feasible (predictable behavior and path of execution) then there would be no reason not to.

所以有没有任何技术原因或不知道的尴尬,这使得这种做法非常常见的原因?

So are there any technical reasons or gotchas that I'm not aware of that makes this practice so commonplace for a reason?

推荐答案

使用匿名函数有三个原因:

I use anonymous functions for three reasons:


  1. 如果不需要名称,因为函数只能在一个地方调用,

  2. 匿名函数声明为内联函数和内联函数,因为它们可以访问父范围中的变量。是的,你可以在匿名函数上放置一个名称,但是如果它被声明为内联函数,通常是无意义的。因此,inline有一个显着的优势,如果你在内联,没有什么理由给它一个名字。

  3. 当处理程序被定义在内部代码似乎更加自包含和可读性调用它们的代码。你可以几乎连续的方式阅读代码,而不必去找那个名字的函数。

我尽量避免深入嵌套的匿名函数,因为可以是毛茸茸的理解和阅读。通常当这种情况发生时,有一个更好的方式来构造代码(有时使用循环,有时使用数据表等等),命名函数通常不是解决方案。

I do try to avoid deep nesting of anonymous functions because that can be hairy to understand and read. Usually when that happens, there's a better way to structure the code (sometimes with a loop, sometimes with a data table, etc...) and named functions isn't usually the solution there either.

我想我还补充说,如果一个回调开始获得超过15-20行的长度,它不需要直接访问父范围中的变量,我会试着给它一个名称,并将它分解成它自己的命名函数声明其他地方。这里肯定有一个可读性点,在这里一个非平凡的函数,获得long只是更易于维护,如果它放在自己的命名单位。但是,大多数回调我最终不是那么长,我发现它更可读,以保持他们内联。

I guess I'd add that if a callback starts to get more than about 15-20 lines long and it doesn't need direct access to variables in the parent scope, I would be tempted to give it a name and break it out into it's own named function declared elsewhere. There is definitely a readability point here where a non-trivial function that gets long is just more maintainable if it's put in its own named unit. But, most callbacks I end up with are not that long and I find it more readable to keep them inline.

这篇关于在JavaScript事件代码中,对回调和参数使用匿名函数而不是命名函数有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆