JavaScript 中函数字面量的确切含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12314905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:53:48  来源:igfitidea点击:

Exact meaning of Function literal in JavaScript

javascriptliteralsfunction-literal

提问by dublintech

In JavaScript there are both Object literals and function literals.

在 JavaScript 中,有对象字面量和函数字面量。

Object literal:

对象字面量:

myObject = {myprop:"myValue"}

Function literal:

函数字面量:

myFunction = function() {
   alert("hello world");
}

What is the significance of the word literal? Can we say Java has method literals?

字面意思是什么意思?我们可以说 Java 有方法文字吗?

public void myMethod() {
    System.out.println("are my literal");
}

采纳答案by Rajeev.Ranjan

A function literal is just an expression that defines an unnamed function.

函数字面量只是一个定义未命名函数的表达式。

The syntax for a function literal is much like that of the function statement, except that it is used as an expression rather than as a statement and no function name is required.

函数字面量的语法与函数语句的语法非常相似,不同之处在于它用作表达式而不是语句,并且不需要函数名称。

So When you give the method name then it can't be a method literal.

因此,当您给出方法名称时,它就不能是方法文字。

回答by vsenol

Add-on:

添加在:

A function literal in JavaScript is a synonym for a function expression.

JavaScript 中的函数字面量是函数表达式的同义词。

Parallel to function expressions, function literals can have an optional identifier (name).

与函数表达式类似,函数文字可以有一个可选的标识符(名称)。

So if we say function expressions / function literals, it includes function expressions / function literals without an identifier (also called anonymous functions), but also function expressions / function literals with an identifier. Even if in a lot of books function expression / function literal is used as a synonym for function expression / function literal without an identifier (anonymous functions).

因此,如果我们说函数表达式/函数字面量,它包括没有标识符的函数表达式/函数字面量(也称为匿名函数),也包括带有标识符的函数表达式/函数字面量。即使在很多书中函数表达式/函数字面量被用作没有标识符的函数表达式/函数字面量的同义词(匿名函数)。

Function Literal

Function objects are created with function literals:

// Create a variable called add and store a function // in it that adds two numbers.

函数字面量

函数对象是用函数字面量创建的:

// 创建一个名为 add 的变量并在其中存储一个 // 将两个数字相加的函数。

> var add = function (a, b) {
>     return a + b; }; 

A function literal has four parts.

The first part is the reserved word function.

The optional second part is the function's name. The function can use its name to call itself recursively. The name can also be used by debuggers and development tools to identify the function. If a function is not given a name, as shown in the previous example, it is said to be anonymous.

The third part is the set of parameters of the function, wrapped in parentheses. Within the parentheses is a set of zero or more parameter names, separated by commas. These names will be defined as variables in the function. Unlike ordinary variables, instead of being initialized to undefined, they will be initialized to the arguments supplied when the function is invoked.

The fourth part is a set of statements wrapped in curly braces. These statements are the body of the function. They are executed when the function is invoked.

A function literal can appear anywhere that an expression can appear...

source: JavaScript: The Good Parts- Douglas Crockford

函数字面量有四个部分。

第一部分是保留字函数。

可选的第二部分是函数的名称。该函数可以使用其名称递归调用自身。调试器和开发工具也可以使用该名称来识别函数。如果一个函数没有命名,如前面的例子所示,它被称为匿名函数。

第三部分是函数的参数集,用括号括起来。括号内是一组零个或多个参数名称,以逗号分隔。这些名称将被定义为函数中的变量。与普通变量不同的是,它们不会被初始化为 undefined,而是会被初始化为调用函数时提供的参数。

第四部分是用花括号包裹的一组语句。这些语句是函数的主体。它们在调用函数时执行。

函数文字可以出现在表达式可以出现的任何地方...

来源:JavaScript:好的部分- Douglas Crockford

That means:

这意味着:

myFunction = function () {
   alert("hello world");
};

is a function expression / function literal, but also:

是一个函数表达式/函数字面量,而且:

myFunction = function myFunction() {
   alert("hello world");
};

is a function expression / function literal.

是一个函数表达式/函数文字。

回答by Salketer

The biggest difference is how/when it is parsed and used. Take your exemple,

最大的区别是如何/何时解析和使用它。以你为例,

myFunction = function() {
   alert("hello world");
}

You can only run myFunction()after the code got to there, since you declare a variable with an anonymous function.

您只能myFunction()在代码到达那里后运行,因为您使用匿名函数声明了一个变量。

If you use the other way,

如果你用另一种方式,

function myFunction(){
   alert("hello world");
}

This function is declared at compile timeand can be used anytime in the scope.

这个函数是在编译时声明的可以在作用域内随时使用。

Please refer to this question also.

也请参考这个问题

回答by Elias Van Ootegem

Don't compare JavaScript with Java, they have about as much in common as a bear and a whale. Java is an object oriented programming language, whereas JavaScript is a functional programming language.

不要将 JavaScript 与 Java 进行比较,它们的共同点就像熊和鲸鱼一样。Java 是一种面向对象的编程语言,而 JavaScript 是一种函数式编程语言。

With a functional language comes the notion of functions as first class objects: functions can be assigned to variables, can be passed as arguments as they can be the return value of other functions.

函数式语言带来了函数作为第一类对象的概念:函数可以分配给变量,可以作为参数传递,因为它们可以作为其他函数的返回值。

An object literal is an object you create on-the-fly and in-line. Same applies for a function literal. But the example you're giving is actually similar to a regular function declaration:

对象字面量是您即时和内联创建的对象。这同样适用于函数文字。但是您给出的示例实际上类似于常规函数声明:

function foo()
{
    alert('bar');
}

Is moved to the top of the scope, where it is convertedto:

被移动到范围的顶部,在那里它被转换为:

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

Makes sense, when functions can be passed as arguments/return values:

当函数可以作为参数/返回值传递时,这是有道理的:

var processed = (function(someFunc)//<-- argument name
{
    return function()
    {
        alert('I\'ll call some function in 2 seconds, get ready');
        setTimeout(someFunc,2000);//<-- passes a reference to foo, as an argument to setTimeout
    }
})(foo);//pass reference to function object foo here

This is only the beginning of all sorts of things you can do with JS, provided you stop treating it as a subset of Java....

这只是你可以用 JS 做的各种事情的开始,只要你停止把它当作 Java 的一个子集......

回答by userA789

No official definition found in ECMA-262.

在 ECMA-262 中没有找到官方定义。

But according to wikipediaand many other PLs I've learnt, literals are expressions of values.

但是根据维基百科和我学到的许多其他 PL,文字是 values 的表达

That means:

这意味着:

function() {alert("hello world")}

is a literal, while:

是文字,而:

function hello_world() {alert("hello world")}

is not. Because the latter expresses not only a value, but a reference.

不是。因为后者不仅表示一个值,而且表示一个引用

I upvoted vsenol's answer, but now I think it is wrong.

我赞成vsenol的回答,但现在我认为这是错误的。

回答by Coconut

A function literalis not a function, but a value of a function.

Afunction literal不是 a function,而是 a 的值function

For an assembly language programmer, it's something like a block of codethat's stored in the .textarea of memory.

对于汇编语言程序员来说,它类似于block of code存储在.text内存区域中的东西。

Then people would want to ask what a functionreally is.

那么人们会想问什么是function真正的。

A functionis actually a pointer or a reference to a function literal, like in any programming language.

Afunction实际上是一个指针或对 a 的引用function literal,就像在任何编程语言中一样。



For example,

例如,

public void myMethod() {
    System.out.println("are my literal");
}

This myMethodis a method.

myMethod是一种方法。

{
    System.out.println("are my literal");
}

And this is a method literal, which Java does not support.

这是一个方法文字,Java 不支持。