Exact meaning of Function literal in JavaScript
Asked Answered
D

6

14

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");
}
Dysentery answered 7/9, 2012 at 8:58 Comment(1)
You also have string/null/boolean/regexp/numerical/array literal. en.wikipedia.org/wiki/Literal_%28computer_programming%29Slothful
R
4

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.

Rhombus answered 7/9, 2012 at 9:3 Comment(1)
If functions were not literal in JavaScript they would always have to have a name. In Java methods are not "literal" because they always have a name. They can never be defined on the fly, pass around or passed into method.Dysentery
N
10

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.

Navigate answered 7/9, 2012 at 9:11 Comment(0)
T
8

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

Tachygraphy answered 7/9, 2012 at 9:13 Comment(0)
M
8

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.

Minnesinger answered 2/10, 2016 at 20:19 Comment(0)
R
4

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.

Rhombus answered 7/9, 2012 at 9:3 Comment(1)
If functions were not literal in JavaScript they would always have to have a name. In Java methods are not "literal" because they always have a name. They can never be defined on the fly, pass around or passed into method.Dysentery
L
1

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.

Letterhead answered 22/7, 2018 at 0:21 Comment(2)
hey, sry but I would prefer according to other resources than wiki.Minnesinger
@Minnesinger tell me if you have more trustworthy general or programming sources than wikipedia.Letterhead
H
0

A function literal is not a function, but is what denotes a value of 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 value of function represented by a function literal as in any programming language.


For example,

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

If we had myMethod which is a method,

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

then this would be the method literal, which Java does not support.

Hickok answered 28/10, 2018 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.