如何记录匿名函数?

发布于 2024-08-27 16:59:17 字数 1431 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那请放手 2024-09-03 16:59:17

理想情况下,匿名函数应该很短并且执行简单的任务。所以......包含它的外部函数应该提供足够的文档。

如果不是这种情况,您可能应该将匿名函数提取到命名函数中,然后正确记录它。

Ideally an anonymous function should be short and perform a straightforward task. So... the outer function that contains it should provide sufficient documentation.

If that is not the case you should likely extract the anonymous function out into a named function, and then properly document it.

疧_╮線 2024-09-03 16:59:17

这并不是真正的匿名函数。它有一个名字。名称是“bar”,或者更具体地说,“foo.bar”。以我的思维方式,这实际上是一个函数文字。按照我的思维方式,真正的匿名函数是一个实际上没有名称的函数,就像另一个函数的参数一样:

var intervalId = setTimeout(function() { // statements }, 1000);

现在,根据《Javascript:权威指南》中的 David Flanagan 的说法,一个匿名函数function 是“用 Function() 构造函数创建的”。不过,他在那里两面下注,因为他进一步指出,此类函数只是“有时”被称为匿名。

我为什么在这里提出这个问题?因为我认为如果你对某件事有一个名字,它就不能真正被称为匿名。我认为关于 Javascript 函数的术语“匿名”充其量是含糊不清的,至少应该由某个地方的人来澄清。

This is not really an anonymous function. It has a name. The name is "bar", or, more specifically, "foo.bar". What this really is, to my way of thinking, is a function literal. To my way of thinking, a truly anonymous function is a function that really does not have a name, like an argument to another function:

var intervalId = setTimeout(function() { // statements }, 1000);

Now, according to David Flanagan in Javascript: The Definitive Guide, an anonymous function is one that is "created with the Function() constructor." He hedges his bets there, though, because he goes on further to state that such functions are only "sometimes" called anonymous.

Why am I raising this issue here? Because I think if you have a name for something, it can't really be called anonymous. And I think the term "anonymous" with respect to Javascript functions is ambiguous at best, and ought to at least be clarified by someone somewhere.

少女净妖师 2024-09-03 16:59:17

通过使其不匿名。

我发现记录某事的最好方法就是不记录。我的意思是我使用良好的变量/函数名称和清晰的代码作为我的文档。

因此,在这种情况下,我只需创建一个静态函数来创建该匿名函数,这样我就可以给该静态函数一个好名字。

By making it not anonymous.

I find the best way to document something is to not to. What I mean I I use good variable/function names and clear code as my document.

So in this case I would just create a static function that creates that anonymous and thus I can give that static function a good name.it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文