“匿名功能”之间的区别和“函数文字”在JavaScript? [英] Difference between "anonymous function" and "function literal" in JavaScript?

查看:205
本文介绍了“匿名功能”之间的区别和“函数文字”在JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本书学习JavaScript 定义匿名函数如下......

The book Learning JavaScript defines anonymous functions as follows...


函数是对象。因此,您可以通过使用构造函数和分配来创建它们 - 就像 String Array 或其他类型一样变量的函数。在下面的代码中,使用 Function 构造函数创建一个新函数,函数体和参数作为参数传入:

Functions are objects. As such, you can create them - just like a String or Array or other type - by using a constructor and assigning the function to a variable. In the following code, a new function is created using the Function constructor, with the function body and argument passed in as arguments:

var sayHi = new Function("toWhom", "alert('Hi' + toWhom);");

这种类型的函数通常被称为匿名函数,因为函数本身不直接声明或命名。

This type of function is often referred to as an anonymous function because the function itself isn't directly declared or named.

这是JavaScript中匿名函数的正确定义吗?如果没有,什么是匿名函数,匿名函数和函数文字之间有什么区别吗?

Is this the correct definition of an "anonymous function" in JavaScript? If not, what is an anonymous function, and is there any difference between an anonymous function and a function literal?

推荐答案

函数表达式和函数声明



由于您对函数感兴趣,这里有一些重要的事情要知道。

Function expressions and function declarations

Since you are interested in functions, here is some important stuff to know.

var abc = function(){...} 称为函数表达式。该变量将在执行时被赋予该匿名函数,但其​​变量声明将被提升到当前执行上下文(范围)的顶部。

var abc = function() { ... } is known as a function expression. The variable will be assigned that anonymous function at execution time, though its variable declaration will be hoisted to the top of the current execution context (scope).

但是,函数表达式也可以赋予名称,以便可以在其体内调用它以使其递归。请记住 IE有一些问题。当你为它指定一个名字时,它绝对是不是一个匿名函数。

However, a function expression can be given a name too, so that it can be called within its body to make it recursive. Keep in mind IE has some issues with this. When you assign it a name, it is most definitely not an anonymous function.

一个函数,如函数abc (){...} 称为函数声明。它的定义被提升到其范围的顶部。它的名称在其中及其父母的范围内可用。

A function such as function abc() { ... } is known as a function declaration. Its definition is hoisted to the top of its scope. Its name is available within it and its parent's scope.

进一步阅读

这是一个匿名函数,但分配给变量 sayHi

It is an anonymous function, but assigned to the variable sayHi.

As ŠimeVidas提到,使用实例化一个新的函数对象新的运算符,参数和函数体作为字符串传入。生成的对象被分配给 sayHi

As Šime Vidas mentions, a new Function object is instantiated with the new operator, and the arguments and function body are passed in as strings. The resulting object is assigned to sayHi.

使用此方法创建函数的实际用途很少(虽然它可能只是为了帮助显示函数是对象)。我还相信将其参数列表和函数体作为字符串传递将调用 eval()类型函数,当更好的构造可用时,这很少有用。

The real world use of creating a function using this method is rare (though it may be just to help show that functions are objects). I also believe passing its arguments list and function body as a string will invoke an eval() type function, which is rarely good when a much better construct is available.

此外,使用函数创建的函数 不形成闭包

如果由于某种原因我需要使用其参数和/或创建函数,我只会使用此方法身体只能作为字符串使用。

I would only use this method if for some reason I needed to create a Function with its arguments and/or body only available to me as a string.

在现实世界中,你会做...

In the real world, you'd do...

var sayHi = function(toWhom) {
   alert('Hi' + toWhom);
};

另请参阅评论 Felix Šime进行了良好的讨论和进一步的澄清。

Also refer to comments by Felix and Šime for good discussion and further clarification.

这篇关于“匿名功能”之间的区别和“函数文字”在JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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