The exact meaning of a functional literal in JavaScript

JavaScript has both Object literals and functional literals.

Object Literal:

myObject = {myprop:"myValue"} 

Functional Literal:

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

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

 public void myMethod() { System.out.println("are my literal"); } 
+8
source share
5 answers

A functional literal is simply an expression that defines an unnamed function.

The syntax for a function literal is very similar to the syntax of a function operator, except that it is used as an expression and not as an operator, and the function name is not required.

So, when you give a method name, it cannot be a method literal.

+1
source

The biggest difference is how / when it is analyzed and used. Take an example

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

You can only run myFunction() after the code has reached it, since you are declaring a variable with an anonymous function.

If you use a different method,

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

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

See this question as well .

+5
source

Do not compare JavaScript with Java, they have about the same as a bear and a whale. Java is an object-oriented programming language, and JavaScript is a functional programming language.

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

A literal object is an object that you create on the fly and in line. The same applies to function literals. But the example you give is actually similar to declaring a regular function:

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

It will move to the top of the area where it is converted to:

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

It 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 just the beginning of all kinds of things you can do with JS if you stop treating it as a subset of Java ....

+5
source

Add:

A functional literal in JavaScript is synonymous with a function expression.

In parallel with function expressions, function literals may have an optional identifier (name).

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

Functional literal

Function objects are created using function literals:

// Create a variable called add and store the function // in it, which adds two numbers.

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

A functional literal has four parts.

The first part is the reserved word function.

The additional second part is the name of the function. A function can use its name for a recursive call. The name can also be used by debuggers and development tools to identify functions. If a function is not named, as shown in the previous example, this is called anonymous.

The third part is a set of function parameters enclosed in parentheses. In 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 initializing to undefined, they will be initialized with the arguments specified when the function was called.

The fourth part is a collection of statements wrapped in braces. These statements are the body of the function. They are executed when the function is called.

A functional literal can appear anywhere where an expression can appear ...

source: JavaScript: Good details - Douglas Crockford

It means:

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

is a character expression of a function / function, as well as:

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

is a function / function literal.

+5
source

There is no official definition in ECMA-262.

But according to wikipedia and many other PLs that I learned, literals are expressions of values .

It means:

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

is literal whereas:

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

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

I approved vsenol answer, but now I think this is wrong.

0
source

Source: https://habr.com/ru/post/924835/


All Articles