简体   繁体   English

在JavaScript中被命名的函数被低估了吗?

[英]Are named functions underrated in JavaScript?

Taking the jQuery framework for example, if you run code like this: 以jQuery框架为例,如果你运行这样的代码:

$(document).ready(function init() { foo.bar(); });

The stack trace you get in Firebug will look like this: 您在Firebug中获得的堆栈跟踪将如下所示:

init()
anonymous()
anonymous([function(), init(), function(), 4 more...], function(), Object name=args)
anonymous()
anonymous()

As you can see, it's not very readable, because you have to click on each function to find out what it is. 正如您所看到的,它不是非常易读,因为您必须单击每个函数以找出它是什么。 The anonymous functions would also show up as (?)() in the profiler, and they can lead to the " cannot access optimized closure " bug. 匿名函数也会在探查器中显示为(?)() ,并且它们可能导致“ 无法访问优化闭包 ”错误。 It seems to me that these are good reasons to avoid them. 在我看来,这些是避免它们的充分理由。 Then there's the fact that ECMAScript 5 will deprecate arguments.callee in its strict mode, which means it won't be possible to reference anonymous functions with it, making them a little less future-proof. 然后就是ECMAScript 5将在其严格模式下弃用arguments.callee这一事实,这意味着它不可能用它来引用匿名函数,这使得它们在未来的证明不那么简单。

On the other hand, using named functions can lead to repetition, eg: 另一方面,使用命名函数可能会导致重复,例如:

var Foo = {
    bar: function bar() {}
}

function Foo() {}

Foo.prototype.bar = function bar() {}

Am I correct in thinking that this repetition is justified in light of the debugging convenience named functions provide, and that the prevalence of anonymous functions in good frameworks like jQuery is an oversight? 我是否认为根据命名函数提供的调试方便,这种重复是正确的,并且jQuery等良好框架中匿名函数的流行是一种疏忽?

I agree there are certain downsides to using anonymous methods in JavaScript/EMCAScript. 我同意在JavaScript / EMCAScript中使用匿名方法有一些缺点。 However, don't overlook how they should be used. 但是,不要忽视它们应该如何使用。 For simple one liners that you want to pass to another function, they are often excellent. 对于想要传递给另一个函数的简单一个衬垫,它们通常非常出色。

I found the answer to my question in this very informative article . 我在这篇内容丰富的文章中找到了我的问题的答案。 Firstly, it turns out that I was right about named functions being more desirable, but the solution is not as simple as adding identifiers to all anonymous functions. 首先,事实证明我认为命名函数更合适是正确的,但解决方案并不像向所有匿名函数添加标识符那么简单。 The main reason for this is JScript implementing function expressions in a very broken way. 主要原因是JScript以非常破碎的方式实现函数表达式。

Secondly, there is a distinction between function statements and expressions. 其次,函数语句和表达式之间存在区别。 An anonymous function is just a function expression with the identifier omitted, and adding an identifier (naming it) wouldn't make it a statement (except in JScript, which is why it's broken). 匿名函数只是一个省略了标识符的函数表达式,添加一个标识符(命名它)不会使它成为一个语句(JScript除外,这就是它被破坏的原因)。 This means that all of the other answers were off mark. 这意味着所有其他答案都不合适。

但对我来说,匿名函数在源代码中更具可读性,因为我确信它们只在那里使用。

Anonymous functions are very convenient. 匿名功能非常方便。 A better fix to this problem, instead of naming the functions, would be if firebug told you on which line in which file the anonymous function was created. 如果firebug告诉你匿名函数创建在哪个文件的哪一行,那么更好地解决这个问题,而不是命名函数。

init()
anonymous() // application.js, line 54
anonymous() // foo.js, line 2

And the stack trace is the only place where anonymous functions are a problem imo. 并且堆栈跟踪是匿名函数成为问题的唯一地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript中的命名和未命名函数 - Named and unnamed functions in JavaScript 命名和未命名的匿名Javascript函数之间的差异 - Differences Between Named and Unnamed Anonymous Javascript Functions 使用Webpack 4在javascript文件中命名的函数 - Named functions in a javascript file using Webpack 4 在javascript中命名vs未命名的函数并理解分配 - Named vs unnamed functions in javascript and understanding allocation javascript中未附加的匿名函数和双命名方法? - Unattached anonymous functions and doubly named methods in javascript? Selenium JavaScript:匿名函数与命名函数 - Selenium JavaScript: Anonymous vs named functions 了解对象文字中的JavaScript匿名函数与命名函数 - Understanding JavaScript anonymous vs. named functions in object literals 如何在coffeescript中为Google Apps脚本生成全局的,命名的javascript函数 - How to generate global, named javascript functions in coffeescript, for Google Apps Script 实际上调用了几个同名的 JavaScript 函数中的哪一个? - Which one of several identically named JavaScript functions is actually called? 尽管Chrome中有条件,但JavaScript会覆盖同名函数 - Javascript Overwriting Same Named Functions Despite Conditionals In Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM