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");
}
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.
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:
Is moved to the top of the scope, where it is converted to:
Makes sense, when functions can be passed as arguments/return values:
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....
A
function literal
is not afunction
, but a value of afunction
.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 afunction literal
, like in any programming language.For example,
This
myMethod
is a method.And this is a method literal, which Java does not support.
The biggest difference is how/when it is parsed and used. Take your exemple,
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,
This function is declared at compile time and can be used anytime in the scope.
Please refer to this question also.
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).
That means:
is a function expression / function literal, but also:
is a function expression / function literal.
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:
is a literal, while:
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.