匿名函数vs正常函数 [英] Anonymous function vs normal function

查看:96
本文介绍了匿名函数vs正常函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数foo(bar){
alert(foo+ bar);
}

  var foo = function(bar){
alert(foo+ bar);
};


解决方案

没有显着的速度差异。 (测试



存在功能差异。




  • 函数声明(如您的第一个函数)和函数表达式(如您的第二个函数)在不同的时间处理。 / li>
  • 它们对它们出现的范围有不同的影响。

  • 您的第一个函数具有真实姓名你的第二个不在ES5和更早的版本中,你的第二个没有;在ES6 / ES2015中,它的确如此,因为规范说JavaScript引擎在这种情况下必须将该变量的名称赋予该函数。


如果你看看函数声明和函数表达式,你会发现话题中的很多谈话(有些甚至是正确的)。



但简要地说:
$ b

函数声明



函数声明就像你的第一个例子发生在执行游标进入其包含的作用域(包含函数或全局作用域)时,在任何分步代码完成之前。因此,它们不能出现在非功能块(<$​​ c $ c> if , try 等)中),因为没有逐步执行步骤代码在处理时已经运行。该函数的名称被添加到它所在的范围中,并且该函数对象具有一个真实的名称(虽然没有标准的方式来查询该名称,但它在堆栈跟踪等方面仍然很有用) 。 (注意:一些JavaScript引擎允许在块内部声明函数,但是它是无效的,它们的作用不一定一致,不要这么做。)



匿名函数表达式



一个函数表达式就像所有的表达式一样,在循序渐进的代码流中遇到。您的表达称为匿名函数表达式,因为它没有明确指定该函数的名称。在ES5及更早版本中,这意味着结果函数没有名字。在ES6 / ES2015及更高版本中,使用匿名函数表达式 do 创建的许多函数都具有名称,因为名称可以从表达式中推断出来,您的示例就是这种情况,函数以命名变量有: foo 。由于匿名函数表达式是表达式,它们可以发生在任何可能出现表达式的地方,尽管有时您必须警告解析器这就是您正在做的事情。



命名函数表达式



还有第三种方法:一个名为的函数表达式,而不是匿名函数表达式。它们看起来像这样:

  var foo = function bar(){
};

  var obj = {
foo:function bar(){
}
};

  doSomething(function bar(){}); 

etc。

他们曾经是确实有问题的跨浏览器(IE8及更早版本将它们混淆;早期版本的Safari有问题等; Kangax的一个好的页面过去常见的问题)。这是一个表达式,所以在任何有表达式的地方都是有效的。在我的示例中,函数名称 bar )不是通过符合JavaScript的引擎添加到包含范围。 / p>

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.

  • Function declarations (like your first) and function expressions (like your second) are processed at different times.
  • They have different effects on the scope in which they occur.
  • Your first function has a true name, your second does not in ES5 and earlier, your second does not; in ES6/ES2015, it does, because the specification says that the JavaScript engine must assign the name of the variable to the function in that case.

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:

Function Declaration

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. 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.)

Anonymous Function Expression

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. 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. 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.

Named Function Expression

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). 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.

这篇关于匿名函数vs正常函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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