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

查看:117
本文介绍了对于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程序员,并且丢弃关于单位代码的Enterprise 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.


  • 灵活性。具有回调参数的异步函数可以通过许多不同的代码路径之一来实现,并且可能不得不编写一个命名函数来解决每种可能的边缘情况。

  • Speed 。它在很大程度上影响了黑客的思想。

  • 其他人都在这样做

  • 文件大小较小,即使是很小的大小,但每一点都取决于

  • 简单的AST?我会假设匿名函数是在运行时生成的,因此JIT不会在将名称映射到指令的过程中大失所望,但是我只是在这一点上猜测。

  • 更快的调度?也不确定这一点。再次猜测。

  • Flexibility. An asynchronous function with a callback parameter 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使用匿名函数的原因有三个:

I use anonymous functions for three reasons:


  1. 如果不需要名称,因为该函数只在一个地方被调用过,那么为什么要添加名称

  2. 将匿名函数声明为内联,并且内联函数的优势在于它们可以访问父作用域中的变量。是的,您可以在匿名函数上放置一个名称,但是如果将其声明为内联函数,则通常毫无意义。因此,内联具有显着的优势,如果您要进行内联,则几乎没有理由在其上加上名称。

  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行,并且它不需要直接访问父作用域中的变量,我很想给它一个名称并将其分解为在其他地方声明的自己的命名函数。这里肯定有一个可读性点,如果将较长的非平凡函数放在自己的命名单元中,则它才更易于维护。但是,我最终遇到的大多数回调都没有那么长,我发现保持内联更加可读。

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天全站免登陆