如何在JavaScript中命名匿名函数有所不同? [英] How does naming an anonymous function in JavaScript make a difference?

查看:113
本文介绍了如何在JavaScript中命名匿名函数有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在分析John Resig网站上的以下两个网址,但我不明白如何给匿名功能命名有所作为。

I am analyzing the following two urls from John Resig's site, but I am not understanding how giving a name to the anonymous function has made a difference.

我的理解是给匿名函数的名称只能在函数定义中使用,并且不在其中,但在以下链接中它会产生巨大差异

My understanding is that the name given to an anonymous function can only be used inside the function definition, and nowhere outside of it, but in the following links it is making a huge difference

  • http://ejohn.org/apps/learn/#13
  • http://ejohn.org/apps/learn/#14

任何解释或参考都会有很大帮助。

Any explanation or reference will be a great help.

我仍然对#14中的以下几行感到困惑。

I am still confused with the following lines in #14

var samurai = { yell: ninja.yell }; 
var ninja = {};
assert( samurai.yell(4) == "hiyaaaa", "The method correctly calls itself." ); 

当ninja现在指向时,Samurai.yell方法如何能够指向ninja.yell到一个空白的对象。

How is Samurai.yell method still able to point ninja.yell when ninja is now pointing to a blank object.

只有#13和#14之间的区别是为#14中的函数表达式提供名称。

Only difference between #13 and #14 is providing a name to the function expression in #14.

ninja.yell是否已经过COPIED而没有被引用或者这些NAMED函数表达式在某些情况下具有全局范围?

Is ninja.yell COPIED to yell and NOT referenced or these kind of NAMED function expression have global scope in some scenario's like this ?

同样的事情发生在#13和#14中,唯一的区别是该功能在#14中命名,未命名在#13加上ninja = #14中的{}和#13中的ninja = null。是否有任何关于NAMED FUNCTION EXPRESSIONS的隐藏概念,这使得#14可行,#13无法使用。

Same thing happens in #13 and #14, only difference is that function is named in #14 and unnamed in #13 plus ninja = {} in #14 and ninja = null in #13. Is there any hidden concept about NAMED FUNCTION EXPRESSIONS that I am missing which makes #14 workable and #13 not workable.

推荐答案

不要试图与Kolink好斗,但他说这不是一个很好的例子。 #14与(在您共享的链接中)有关的是命名函数表达式(与函数声明不同的动物)。无论函数引用的传递位置如何,如果为函数表达式命名,它总是有一种从内部调用自身的方法。这个名称,你给你的函数表达式,是一个只有它知道的名称;它不存在于任何外部范围内。

Not trying to be combative with Kolink, but he goes a bit too far in saying it is NOT a good example. What #14 has to do with (in the links you shared) are named function expressions (a different animal from function declarations). Regardlesss of where the function reference is passed, if you name your function expression, it will always have a way to call itself, from within itself. This name, that you give your function expression, is a name that only it knows; it does not exist in any external scope.

参见此处此处 on MDN,关于函数表达式与函数声明的进一步讨论。底部的第二个链接有一个关于命名函数表达式的标题。 确实有用;请参阅我的要点,了解一次性递归函数的示例,它不会向本地或全局变量范围添加任何内容(例如,对于一次性DOM遍历很有用。)

See here and here on MDN, for a further discussion about function expressions vs. function declarations. The second link, at the bottom, has a heading about named function expressions. It does have a use; see my Gist for an example of one-off recursive function, that adds nothing to the local or global variable scope (useful for one-off DOM traversal, for instance).

此外,Tobias(在他的答案中)指出了命名函数表达式的其他好用法,即in调试。

Also, Tobias (in his answer here) points out other good uses of named function expressions, namely, in debugging.

这篇关于如何在JavaScript中命名匿名函数有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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