是什么决定了JavaScript函数是一个命名的匿名函数还是一个常规函数? [英] What determines if a JavaScript function is a named anonymous function versus a, um, regular function?

查看:116
本文介绍了是什么决定了JavaScript函数是一个命名的匿名函数还是一个常规函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读重新介绍JavaScript我注意到有关函数的一些有趣内容:

Reading "A re-introduction to JavaScript" I noticed something interesting about functions:


如上所述提供给匿名函数的名称(或者至少应该是)仅可用于函数自己的范围。

The name provided to an anonymous function as above is(or at least should be) only available to the function's own scope.

根据nodejs提示中教程中的代码输入一些内容我能够验证节点是否同意与作者:

Entering some things based on the code in the tutorial at the nodejs prompt I was able to verify that node agrees with the author:

function add(foo, bar) {
  return foo + bar;
}

add(1, 2);

给我3,并且:

var five = (function plus(foo, bar) {
             return foo + bar;
           })(2, 3);
plus(2, 3);

给我一​​个关于加号未定义的语法错误。

gets me a syntax error about plus not being defined.

我有点困惑,因为我用来定义两个函数的代码是相同的(名称除外)。 JavaScript如何知道第一个是常规函数,第二个是命名匿名函数?

I'm a little confused because the code I used to define both functions was identical (except for the name). How does JavaScript know the first one is a regular function and the second one is a named anonymous function?

推荐答案

第一个是正常的功能声明。声明的名称被引入当前作用域,并隐式引用函数体。在这种形式中,函数语句,因此不返回任何值。

The first is a normal function declaration. The declared name is introduced into the current scope, and implicitly refers to the function body. In this form, function is a statement, and as such returns no value.

第二个是函数表达式,并且解释器知道这是因为它是赋值的右侧的一部分,并且由于它周围的括号。每当单词 function 出现时,可能会提供其他一些值(或表达式),那么你所拥有的是一个函数表达式。该表达式的结果是对函数体的引用。

The second is a function expression, and the interpreter knows that because it's part of the right hand side of an assignment, and because of the braces around it. Any time the word function appears where some other "value" (or "expression") could have been supplied then what you have is a function expression. The result of that expression is a reference to the function body.

函数表达式的名称(如果它被赋予一个)仅是在函数中可以,如链接中所述。

The name of a function expression (if it were given one) is only available within the function, as described in your link.

恕我直言,该链接中的术语不正确。在我看来,他们所谓的命名匿名函数应该被称为命名函数表达式。这个明显的错误可能源于这样一个事实,即所有匿名函数实际上都是函数表达式,但并非所有函数表达式都是匿名的。

IMHO the nomenclature in that link is incorrect. What they call a "named anonymous functions" should in my view just be called a "named function expression". This apparent error may stem from the fact that all anonymous functions are actually function expressions, but not all function expressions are anonymous.

注意,如果你使用这种风格:

Note that if you use this style:

var foo = function bar() {
    ...
}

然后如上所述 bar 是一个函数表达式,其结果随后被赋值给变量 foo 。名称 bar 未放入当前范围,但在函数本身中可用。

then as described above bar is a function expression, whose result is then assigned to the variable foo. The name bar is not put into the current scope, but is available within the function itself.

声明变量 foo 将被提升到作用域的顶部,但不会被分配其值(即对函数表达式的引用),直到实际执行上面的行。

The declaration of the variable foo will be hoisted to the top of the scope, but will not be assigned its value (i.e. the reference to the function expression) until the line above is actually executed.

差异的另一个有趣的证明是(来自Chrome控制台):

Another interesting demonstration of the difference is this (from the Chrome console):

> function() { }
SyntaxError: Unexpected token (
> a = function() { }
function () { }

请注意,唯一的区别是后者有一个分配给 a 。试图宣布一个使用语句的匿名函数是非法的,因为语言语法要求函数语句包含函数的标签。但后者隐式是函数表达式,允许匿名。

Note that the only difference is that in the latter there is an assignment to a. The attempt to declare an anonymous function using a statement is illegal, because the language grammar requires that the function statement include a label for the function. However the latter is implicitly a function expression, which is allowed to be anonymous.

这篇关于是什么决定了JavaScript函数是一个命名的匿名函数还是一个常规函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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