Exact meaning of Function literal in JavaScript

2019-04-06 06:52发布

问题:

In JavaScript there are both Object literals and function literals.

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?

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

回答1:

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.



回答2:

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.

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 converted to:

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



回答3:

Add-on:

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

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.

> 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

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.



回答4:

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.

If you use the other way,

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

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

Please refer to this question also.



回答5:

No official definition found in ECMA-262.

But according to wikipedia and many other PLs I've learnt, literals are expressions of 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.



回答6:

A function literal is not a function, but a value of a function.

For an assembly language programmer, it's something like a block of code that's stored in the .text area of memory.

Then people would want to ask what a function really is.

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


For example,

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

This myMethod is a method.

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

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