[英]Anonymous function vs normal function
Just out of interest, are there any speed/functionality differences between 只是出于兴趣,它们之间是否有任何速度/功能差异
function foo(bar) {
alert("foo" + bar);
}
and 和
var foo = function(bar) {
alert("foo" + bar);
};
There are no significant speed differences. 没有明显的速度差异。 ( Test )
( 测试 )
There are functionality differences. 存在功能差异。
If you look around for "function declaration" vs. "function expression" you'll find a lot of talk (some of it even correct) on the topic. 如果你四处寻找“函数声明”和“函数表达”,你会发现很多关于这个主题的讨论(其中一些甚至是正确的)。
But briefly: 但简单地说:
A function declaration like your first example happens when the execution cursor enters its containing scope (containing function or the global scope), before any step-by-step code is done. 在执行任何分步代码之前,执行游标进入其包含范围(包含函数或全局范围)时,会发生类似于第一个示例的函数声明 。 Therefore they cannot appear within non-function blocks (
if
, try
, etc.), since no step-by-step code has been run when they're processed. 因此,它们不能出现在非功能块中(
if
, try
等),因为在处理它们时没有运行逐步代码。 The name of the function is added to the scope in which it appears, and the function object has a true name (although there's no standard way to query that name, it's still useful in stack traces and such). 函数的名称被添加到它出现的范围中,并且函数对象具有真实的名称(尽管没有标准的方法来查询该名称,它在堆栈跟踪等方面仍然有用)。 (Note: Some JavaScript engines allow function declarations within blocks, but it's invalid and what they do is not necessarily consistent. Don't do it.)
(注意:有些JavaScript引擎允许在块内进行函数声明,但它无效 ,它们的作用不一定一致。不要这样做。)
A function expression like your second example happens, like all expressions, when it's encountered in the step-by-step flow of the code. 像第二个例子一样的函数表达式就像所有表达式一样,在代码的逐步流程中遇到它。 Your expression is called an anonymous function expression since it doesn't explicitly specify a name for the function.
您的表达式称为匿名函数表达式,因为它没有显式指定函数的名称。 In ES5 and earlier, that meant that the resulting function had no name.
在ES5及更早版本中,这意味着生成的函数没有名称。 In ES6/ES2015 and later, many functions created with anonymous function expressions do have names because the name can be inferred from the expression, and that's the case with your example, in which the function ends up with the name the variable has:
foo
. 在ES6 / ES2015及更高版本中,使用匿名函数表达式创建的许多函数都具有名称,因为名称可以从表达式中推断出来,而您的示例就是这种情况,其中函数以变量的名称结尾:
foo
。 Since anonymous function expressions are expressions, they can occur anywhere expressions can occur, although sometimes you have to warn the parser that that's what you're doing. 由于匿名函数表达式是表达式,它们可以出现在表达式可以发生的任何地方,尽管有时您必须警告解析器这就是您正在做的事情。
There's a third way of doing this: A named function expression, rather than an anonymous one. 还有第三种方法: 命名函数表达式,而不是匿名函数表达式。 They look like this:
它们看起来像这样:
var foo = function bar() {
};
or 要么
var obj = {
foo: function bar() {
}
};
or 要么
doSomething(function bar() { });
etc. 等等
They used to be really problematic cross-browser (IE8 and earlier mess them up , for instance; early versions of Safari had issues, etc.; Kangax has a good page of the problems that used to abound). 他们曾经是真正有问题的跨浏览器(IE8和更早版本混乱起来 ,例如,Safari浏览器的早期版本有问题,等等; Kangax具有良好的页面中所使用比比皆是的问题)。 It's an expression, so it's valid anywhere there's an expression.
它是一个表达式,因此只要有表达式,它就是有效的。 The function name (
bar
in my example) is not added to the containing scope by a compliant JavaScript engine. 函数名称 (我的示例中的
bar
) 未被兼容的JavaScript引擎添加到包含范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.