Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exact meaning of Function literal in JavaScript

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");
}
like image 443
dublintech Avatar asked Sep 07 '12 08:09

dublintech


People also ask

What is object literal in JavaScript MDN?

Objects can be initialized using new Object() , Object. create() , or using the literal notation (initializer notation). An object initializer is a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ( {} ).

What is the difference between a function expression and a function declaration?

The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions. A function expression can be used as an IIFE (Immediately Invoked Function Expression) which runs as soon as it is defined.

Which function is used to check the type of the literal?

Answer. Answer: Python has an in-built function called type() which we can use to determine the data type of the literal.

What are object literals in JavaScript?

JavaScript Object Literals can have properties that are also objects, and those objects have their own context. In each case, when a function executes within that context, inside of the function, the “ this ” keyword refers to the object that the function is a property of, because the function executes in the context of that object.

What is a function literal?

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

What does “this” mean in object literals?

When you instantiate a constructor function, inside of the instance object, “this” refers to the instance object So, as you can see, “this” can easily give you a headache. But hang in there; we are getting to the good stuff now. In a nutshell, in Object-literals, you don’t have local variables; you have properties of the object.

What is the syntax for a method literal?

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.


1 Answers

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.

like image 95
Salketer Avatar answered Sep 22 '22 21:09

Salketer