Anonymous vs. Named

Scope&Closure 里面有这个章节,翻译一下

Anonymous function expressions are quick and easy to type, and many libraries and tools tend to encourage this idiomatic style of code. However, they have several draw-backs to consider:

1.Anonymous functions have no useful name to display in stack traces, which can make debugging more difficult.

2.Without a name, if the function needs to refer to itself, for recursion, etc., the deprecated (arguments.callee) reference is unfortunately required. Another example of needing to self-reference is when an event handler function wants to unbind itself after it fires.

3.Anonymous functions omit a name that is often helpful in providing more readable/understandable code. A descriptive name helps self-document the code in question.

匿名方法写起来很爽,许多类库工具都倾向这种写法,然而它也有一些缺点:

1.匿名方法在stack trace(debug的堆栈信息)中是没有名字的,所以在调试当中会比有名字的方法更困难。

2.如果方法没有名字,那么当它要执行递归等操作的时候,就需要调用废弃掉的(arguments.callee)方法。还有一个需要关联自身的场合,就是当事件触发之后,方法想要解绑。

3.匿名方法通常可读性都不是很好,

你可能感兴趣的:(Anonymous vs. Named)